我不能用 Get it in Flutter 来消费 Bloc



我试图用GetIt消费一个区块,但我得到了这个错误:

错误:在此BlocBuilder<之上找不到正确的提供程序;GetUserProfileBlock,GetUserProfileState>小工具

发生这种情况是因为您使用了不包括提供程序的BuildContext由您选择。有几个常见的场景:

GetIt不是应该在没有提供者的情况下消费区块吗?我该怎么修?

以下是我的代码:

void main() {
setupServiceLocator();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home:  MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late GetUserProfileBloc bloc;
@override
void initState() {
super.initState();
bloc = sl.get<GetUserProfileBloc>();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: BlocBuilder<GetUserProfileBloc, GetUserProfileState>(builder: (context, state) {
if (state is GetUserProfileInitialState) {
return const Center(child: Text("initial"));
} else if (state is GetUserProfileLoadingState) {
return const Center(child: CircularProgressIndicator());
} else if (state is GetUserProfileSuccessState) {
return Center(child: Text(state.userProfile.toString()),);
} else {
return const Center(child: Text("sssssss"),);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () async {
bloc.add(GetUserProfileEvent());
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

我从来没有用过那个方法。试试这个,它有效。如果你很难获得上下文,那么用final scaffoldKey=GlobalKey((绑定你的脚手架;将其放入脚手架参数scaffold(key:scaffoldKey,(;使用scaffoldKey,您可以获得当前上下文!

late PostBloc bloc;
@override
Widget build(BuildContext context) {
bloc = BlocProvider.of<PostBloc>(context);
}

或者使用这个技巧

@Override    
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => SomeServiceBloc()..add(CheckLocationOnEnd())),
)], child .... );

调用getIt并不意味着BlocBuilder可以在构建上下文中找到块。若要解决此问题,请使用getIt向生成器提供块。

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late GetUserProfileBloc bloc;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: BlocBuilder<GetUserProfileBloc, GetUserProfileState>(
bloc: sl.get<GetUserProfileBloc>(),
builder: (context, state) {
if (state is GetUserProfileInitialState) {
return const Center(child: Text("initial"));
} else if (state is GetUserProfileLoadingState) {
return const Center(child: CircularProgressIndicator());
} else if (state is GetUserProfileSuccessState) {
return Center(child: Text(state.userProfile.toString()),);
} else {
return const Center(child: Text("sssssss"),);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () async {
bloc.add(GetUserProfileEvent());
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

最新更新