Flutter 错误:找不到正确的提供程序<;NavBloc>;在这个BlocBuilder之上<;NavBloc,NavState>;小装置
现在,我使用flutter状态管理器定义bloc状态管理文件。当我像这样使用集团时:Flutter 错误:找不到正确的提供程序<;NavBloc>;在这个BlocBuilder之上<;NavBloc,NavState>;小装置,flutter,bloc,Flutter,Bloc,现在,我使用flutter状态管理器定义bloc状态管理文件。当我像这样使用集团时: import 'package:acientbay/src/bloc/nav/nav_bloc.dart'; import 'package:acientbay/src/page/home/home_list.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'packag
import 'package:acientbay/src/bloc/nav/nav_bloc.dart';
import 'package:acientbay/src/page/home/home_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
class HomeNav extends HookWidget{
@override
Widget build(BuildContext context) {
return BlocBuilder<NavBloc, NavState>(
buildWhen: (previous, current) => previous.username != current.username,
builder: (context, state) {
return Scaffold(
body: HomeList(),
bottomNavigationBar: BottomNavigationBar(
items:[
BottomNavigationBarItem(icon: Icon(Icons.home), label: '作品库'),
BottomNavigationBarItem(icon: Icon(Icons.school), label: '我的')
],
currentIndex:state.selectNavIndex
),
);
},
);
}
}
Scaffold(
body: BlocProvider(
create: (context) => NavBloc(),
child: ...(), // Your class goes here
),
)
import'包:acientbay/src/bloc/nav/nav_bloc.dart';
导入“package:acientbay/src/page/home/home_list.dart”;
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:颤振团/颤振团.飞镖”;
进口“包装:颤振钩/颤振钩.省道”;
类HomeNav扩展了HookWidget{
@凌驾
小部件构建(构建上下文){
返回BlocBuilder(
buildWhen:(previous,current)=>previous.username!=current.username,
生成器:(上下文、状态){
返回脚手架(
正文:HomeList(),
底部导航栏:底部导航栏(
项目:[
BottomNavigationBarItem(图标:图标(Icons.home)),标签:'作品库'),
BottomNavigationBarItem(图标:图标(Icons.school)),标签:'我的')
],
currentIndex:state.selectNavIndex
),
);
},
);
}
}
显示错误:
Performing hot restart...
Syncing files to device sdk gphone x86 arm...
Restarted application in 952ms.
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building HomeNav:
Error: Could not find the correct Provider<NavBloc> above this BlocBuilder<NavBloc, NavState> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that BlocBuilder<NavBloc, NavState> is under your MultiProvider/Provider<NavBloc>.
This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
```
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>()),
),
}
```
consider using `builder` like so:
```
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) {
// No longer throws
return Text(context.watch<Example>()),
}
),
}
```
If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
HomeNav file:///home/dolphin/Documents/GitHub/acientbay/lib/src/app/acientbay_app.dart:12:12
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:332:7)
#1 Provider.of (package:provider/src/provider.dart:284:30)
#2 ReadContext.read (package:provider/src/provider.dart:610:21)
#3 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57)
...
====================================================================================================
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building HomeNav:
Error: Could not find the correct Provider<NavBloc> above this BlocBuilder<NavBloc, NavState> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that BlocBuilder<NavBloc, NavState> is under your MultiProvider/Provider<NavBloc>.
This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
```
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>()),
),
}
```
consider using `builder` like so:
```
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) {
// No longer throws
return Text(context.watch<Example>()),
}
),
}
```
If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
HomeNav file:///home/dolphin/Documents/GitHub/acientbay/lib/src/app/acientbay_app.dart:12:12
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:332:7)
#1 Provider.of (package:provider/src/provider.dart:284:30)
#2 ReadContext.read (package:provider/src/provider.dart:610:21)
#3 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57)
...
====================================================================================================
正在执行热重启。。。
正在将文件同步到设备sdk gphone x86 arm。。。
在952ms内重新启动应用程序。
=======小部件库捕获的异常=======================================================
在生成HomeNav时引发了以下ProviderNotFoundException:
错误:在此BlocBuilder小部件上方找不到正确的提供程序
发生这种情况是因为您使用了不包含提供程序的“BuildContext”
由您选择。有几种常见情况:
-您在“main.dart”中添加了一个新的提供程序,并执行了热重新加载。
要修复此问题,请执行热重启。
-您试图读取的提供程序位于不同的路径中。
提供者是“范围化”的。因此,如果在路由中插入提供者,则
其他路由将无法访问该提供商。
-您使用了“BuildContext”,它是您试图读取的提供程序的祖先。
确保BlocBuilder在您的MultiProvider/Provider下。
这通常发生在创建提供者并尝试立即读取它时。
例如,而不是:
```
小部件构建(构建上下文){
返回提供者(
创建:()=>Example(),
//将抛出ProviderNotFoundError,因为'context'已关联
//指向作为“提供者”父级的小部件`
子:文本(context.watch()),
),
}
```
考虑使用“Builder”这样:
```
小部件构建(构建上下文){
返回提供者(
创建:()=>Example(),
//我们使用“builder”来获取一个新的“BuildContext”,它可以访问提供者
生成器:(上下文){
//不再投掷
返回文本(context.watch()),
}
),
}
```
如果这些解决方案都不起作用,请考虑在StAcExpLoad上寻求帮助:
https://stackoverflow.com/questions/tagged/flutter
导致错误的相关小部件是:
家庭导航file:///home/dolphin/Documents/GitHub/acientbay/lib/src/app/acientbay_app.dart:12:12
引发异常时,这是堆栈:
#0提供程序。\u继承了elementof(包:Provider/src/Provider.dart:332:7)
#1 Provider.of(包:Provider/src/Provider.dart:284:30)
#2 ReadContext.read(包:provider/src/provider.dart:610:21)
#3\u BlocBuilderBaseState.initState(包:flatter\u bloc/src/bloc\u builder.dart:130:36)
#4 StatefulElement.\u firstBuild(包:flatter/src/widgets/framework.dart:4711:57)
...
====================================================================================================
=======小部件库捕获的异常=======================================================
在生成HomeNav时引发了以下ProviderNotFoundException:
错误:在此BlocBuilder小部件上方找不到正确的提供程序
发生这种情况是因为您使用了不包含提供程序的“BuildContext”
由您选择。有几种常见情况:
-您在“main.dart”中添加了一个新的提供程序,并执行了热重新加载。
要修复此问题,请执行热重启。
-您试图读取的提供程序位于不同的路径中。
提供者是“范围化”的。因此,如果在路由中插入提供者,则
其他路由将无法访问该提供商。
-您使用了“BuildContext”,它是您试图读取的提供程序的祖先。
确保BlocBuilder在您的MultiProvider/Provider下。
这通常发生在创建提供者并尝试立即读取它时。
例如,而不是:
```
小部件构建(构建上下文){
返回提供者(
创建:()=>Example(),
//将抛出ProviderNotFoundError,因为'context'已关联
//指向作为“提供者”父级的小部件`
子:文本(context.watch()),
),
}
```
考虑使用“Builder”这样:
```
小部件构建(构建上下文){
返回提供者(
创建:()=>Example(),
//我们使用“builder”来获取一个新的“BuildContext”,它可以访问提供者
生成器:(上下文){
//不再投掷
返回文本(context.watch()),
}
),
}
```
如果这些解决方案都不起作用,请考虑在StAcExpLoad上寻求帮助:
https://stackoverflow.com/questions/tagged/flutter
导致错误的相关小部件是:
家庭导航file:///home/dolphin/Documents/GitHub/acientbay/lib/src/app/acientbay_app.dart:12:12
引发异常时,这是堆栈:
#0提供程序。\u继承了elementof(包:Provider/src/Provider.dart:332:7)
#1 Provider.of(包:Provider/src/Provider.dart:284:30)
#2 ReadContext.read(包:provider/src/provider.dart:610:21)
#3\u BlocBuilderBaseState.initState(包:flatter\u bloc/src/bloc\u builder.dart:130:36)
#4 StatefulElement.\u firstBuild(包:flatter/src/widgets/framework.dart:4711:57)
...
=======