Flutter 使用不包含MyBloc类型的Bloc/Cubit的上下文调用BlocProvider.of()
我试图使用BlocProvider添加MyBloc,但遇到了上下文问题。错误如下: 正在调试模式下在sdk gphone x86 arm上启动lib\main.dart。。。 lib\main.dart:1 √ 内置build\app\outputs\flatter apk\app-debug.apk。 正在连接到位于ws://127.0.0.1:64346/zfCDazZFoQI=/ws的VM服务Flutter 使用不包含MyBloc类型的Bloc/Cubit的上下文调用BlocProvider.of(),flutter,dart,bloc,Flutter,Dart,Bloc,我试图使用BlocProvider添加MyBloc,但遇到了上下文问题。错误如下: 正在调试模式下在sdk gphone x86 arm上启动lib\main.dart。。。 lib\main.dart:1 √ 内置build\app\outputs\flatter apk\app-debug.apk。 正在连接到位于ws://127.0.0.1:64346/zfCDazZFoQI=/ws的VM服务 ════════ Exception caught by gesture ═══════════
════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc.
No ancestor could be found starting from the context that was passed to BlocProvider.of<MyBloc>().
This can happen if the context you used comes from a widget above the BlocProvider.
The context used was: BlocAddPage(state: _BlocAddPageState#43fc1)
When the exception was thrown, this was the stack
#0 BlocProvider.of
package:flutter_bloc/src/bloc_provider.dart:121
#1 _BlocAddPageState.build.<anonymous closure>
package:example/…/examples/bloc_add.dart:53
#2 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:993
#3 _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1111
#4 GestureRecognizer.invokeCallback
我有以下UI类
class BlocAddPage extends StatefulWidget {
BlocAddPage({Key key}) : super(key: key);
static const routeName = '/addBloc';
@override
_BlocAddPageState createState() => _BlocAddPageState();
}
class _BlocAddPageState extends State<BlocAddPage> {
final Repository repository = Repository();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Add',
),
),
body: BlocProvider<MyBloc>(
create: (context) => MyBloc(
repository: context.read<Repository>(),
child: ElevatedButton(
onPressed: () {
BlocProvider.of<MyBloc>(context)
.add(AddEvent());
},
child: const Text('Submit'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.all(15),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(40),
),
),
)),
问题在于你的背景。用生成器包装 原因:调用BlocProvider.ofcontext时,上下文的上下文位于提供程序下面的某个位置。但是在您的例子中,上下文是_BlocAddPageState的上下文,它是提供者的祖先。因此,我提出了一个建设者来解决它
class BlocAddPage extends StatefulWidget {
BlocAddPage({Key key}) : super(key: key);
static const routeName = '/addBloc';
@override
_BlocAddPageState createState() => _BlocAddPageState();
}
class _BlocAddPageState extends State<BlocAddPage> {
final Repository repository = Repository();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Add',
),
),
body: BlocProvider<MyBloc>(
create: (context) => MyBloc(
repository: context.read<Repository>(),
child: Builder( // this!!!
builder: (context) => ElevatedButton(
onPressed: () {
BlocProvider.of<MyBloc>(context)
.add(AddEvent());
},
child: const Text('Submit'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.all(15),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(40),
),
),
))),
问题在于你的背景。用生成器包装 原因:调用BlocProvider.ofcontext时,上下文的上下文位于提供程序下面的某个位置。但是在您的例子中,上下文是_BlocAddPageState的上下文,它是提供者的祖先。因此,我提出了一个建设者来解决它
class BlocAddPage extends StatefulWidget {
BlocAddPage({Key key}) : super(key: key);
static const routeName = '/addBloc';
@override
_BlocAddPageState createState() => _BlocAddPageState();
}
class _BlocAddPageState extends State<BlocAddPage> {
final Repository repository = Repository();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Add',
),
),
body: BlocProvider<MyBloc>(
create: (context) => MyBloc(
repository: context.read<Repository>(),
child: Builder( // this!!!
builder: (context) => ElevatedButton(
onPressed: () {
BlocProvider.of<MyBloc>(context)
.add(AddEvent());
},
child: const Text('Submit'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.all(15),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(40),
),
),
))),
plz show error.added exception plz show error.added exception银行兄弟,我也有同样的问题。祝贺你们俩guys@SebastianCorradi很高兴听到这个!谢谢兄弟,我也有同样的问题。祝贺你们俩guys@SebastianCorradi很高兴听到这个!