提问人:Karthikn 提问时间:11/16/2023 更新时间:11/17/2023 访问量:38
无法在此 BlocBuilder<WelcomeBloc, WelcomeState> 小部件上方找到正确的 Provider<WelcomeBloc>
Could not find the correct Provider<WelcomeBloc> above this BlocBuilder<WelcomeBloc, WelcomeState> Widget
问:
我解决了错误类型不匹配的问题,但现在抛出了另一个异常
不能将参数类型“List<dynamic>”分配给参数类型“List<SingleChildWidget>”
这是控制台输出
════════ 小部件库捕获的异常═══════════════════════════════════ 引发了以下 ProviderNotFoundException 生成 Welcome(state: _WelcomeState#276b6): 错误:无法在此 BlocBuilder<WelcomeBloc、WelcomeState> 小部件上方找到正确的提供程序
发生这种情况的原因是您使用了不包含提供程序的
由您选择。有几种常见方案:BuildContext
您在 中添加了新的提供程序并执行了热重载。 要解决此问题,请执行热重启。
main.dart
您尝试读取的提供程序位于不同的路由中。
提供程序是有“范围”的。因此,如果您在路由中插入 of provider,那么 其他路由将无法访问该提供程序。
您使用了您尝试读取的提供程序的祖先。
BuildContext
确保 BlocBuilder<WelcomeBloc, WelcomeState> 位于 MultiProvider/Provider 下。 这通常发生在创建提供程序并尝试立即读取它时。
例如,代替:
Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // Will throw a ProviderNotFoundError, because `context` is associated // to the widget that is the parent of `Provider<Example>` child: Text(context.watch<Example>().toString()), ); }
考虑像这样使用:
builder
Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // we use `builder` to obtain a new `BuildContext` that has access to the provider builder: (context, child) { // No longer throws return Text(context.watch<Example>().toString()); } ); }
导致错误的相关小部件是
欢迎页面.dart:19
当异常被抛出时,这是堆栈。
这是我修改后的main.dart文件
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: AppPages.allBlocProviders(context) ,
child: ScreenUtilInit(
builder: (context, child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
iconTheme: IconThemeData(
color: AppColors.primaryText
),
elevation: 0,
backgroundColor: Colors.white
)
),
onGenerateRoute: AppPages.GenerateRouteSettings,
);
},
)
);
}
这是我的welcome.dart文件。
return BlocBuilder<WelcomeBloc, WelcomeState>(
builder: (context, state) {
return Container(
color: Colors.white,
child: Scaffold(
body: Container(
margin: EdgeInsets.only(top: 34.h),
width: 375.w,
child: Stack(
alignment: Alignment.topCenter,
children: [
PageView(
controller: pageController,
onPageChanged: (value) {
state.page = value;
BlocProvider.of<WelcomeBloc>(context).add(WelcomeEvent());
// print(value);
},
children: [
_page(
1,
context,
"Next",
"First See Learning",
"Forget about a for of paper all knowledge in on learning",
"assets/images/reading.png"
),
_page(
2,
context,
"Next",
"Connect With Everyone",
"Always keep in touch with your tutor & friend, Let's get connected",
"assets/images/boy.png"
),
_page(
3,
context,
"Get Started",
"Always Fascinated Learning",
"Anywhere, anytime, The time is at our direction so study whenever you want",
"assets/images/man.png"
),
],
),
Positioned(
bottom: 100.h,
child: DotsIndicator(
position: state.page,
dotsCount: 3,
mainAxisAlignment: MainAxisAlignment.center,
decorator: DotsDecorator(
color: AppColors.primaryThirdElementText,
activeColor: AppColors.primaryElement,
size: const Size.square(8.0),
activeSize: const Size(18.0, 8.0),
activeShape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)
)
),
)
)
],
),
),
),
);
},
);
}
这是我修改后的pages.dart文件
static List<PageEntity> routes (){
return [
PageEntity(
route: AppRoutes.INITIAL,
page: const Welcome(),
bloc: BlocProvider(create: (context)=>WelcomeBloc(),)
),
PageEntity(
route: AppRoutes.SING_IN,
page: const SignInPage(),
bloc: BlocProvider(create: (context)=>SignInBlocs(),)
),
PageEntity(
route: AppRoutes.REGISTER,
page: const Register(),
bloc: BlocProvider(create: (context)=>RegisterBlocs(),)
),
];
}
// return all the bloc providers
static List<BlocProvider> allBlocProviders(BuildContext context){
List<BlocProvider> blocProviders = <BlocProvider>[];
for(var blocs in routes()){
if(blocs.bloc != null){
blocProviders.add(blocs.bloc!);
}
}
return blocProviders;
}
// model that cover entire screen as we click on navigator object
static MaterialPageRoute GenerateRouteSettings(RouteSettings settings){
if(settings.name !=null){
// check for route name matching when navigator gets triggered.
var result = routes().where((element) => element.route == settings.name);
if(result.isNotEmpty){
return MaterialPageRoute(builder: (_)=> result.first.page, settings: settings);
}
}
return MaterialPageRoute(builder: (_)=> const SignInPage(), settings: settings);
}
}
class PageEntity{
String route;
Widget page;
BlocProvider? bloc;
PageEntity({required this.route, required this.page, this.bloc});
}
如何处理此错误。我累了,但找不到解决方案。
答:
似乎没有问题。你能提供一个示例供我调试吗?
下一个:在函数的签名中抛出关键字
评论
BlocBuilder<WelcomeBloc>
BlocProvider<WelcomeBloc>()