找不到正确的提供者不一致的行为
Could not find the correct Provider inconsistent behaviour
嗨,我是 Flutter 和 Provider 的新手,无法理解这个错误。基本上,我使用 MultiProvider
来管理如下状态,这对其中一个(Auth)非常有用,但对其他(User)却不起作用,即使我以相同的方式使用它们。
我收到这个错误。
Note there are actually more providers, but simplified it for sake of simpler code example
错误
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following ProviderNotFoundException was thrown building StartPage(dirty, dependencies: [_InheritedProviderScope<Auth>, _InheritedProviderScope<UserLocation>, _InheritedProviderScope<UserState>, _InheritedProviderScope<BottomNavigationBarProvider>]):
Error: Could not find the correct Provider<User> above this StartPage Widget
To fix, please:
* Ensure the Provider<User> is an ancestor to this StartPage Widget
* Provide types to Provider<User>
* Provide types to Consumer<User>
* Provide types to Provider.of<User>()
* Ensure the correct `context` is being used.
If none of these solutions work, please file a bug at:
Main.dart
class SchoolApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Auth()),
ChangeNotifierProvider(create: (_) => User()),
],
child: HomePage(),
));
}
}
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
UserState _userState = Provider.of<UserState>(context);
switch (_userState.status) {
case UserStatus.UnAuthenticated:
return LoginScreen();
case UserStatus.Authenticated:
return StartPage();
}
}
}
StartPage.dart
class StartPage extends StatelessWidget with ChangeNotifier {
Timer timer;
@override
Widget build(BuildContext context) {
final _auth = Provider.of<Auth>(context);
final _user = Provider.of<User>(context, listen: true);
...
User.dart
class User extends ChangeNotifier{
Firestore db = Firestore.instance;
String _userId, _firstName, _lastName, _school, _email, _description, _employer, _title, _name;
List<String> _ideologies, _interests, _religions;
UserType _userType;
...
Auth.dart
class Auth extends ChangeNotifier {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signInWithEmailAndPassword(String email, String password) async {
final AuthResult authResult = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
notifyListeners();
return authResult.user.uid.toString();
}
...
class SchoolApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Auth>(create: (_) => Auth()),
ChangeNotifierProvider<User>(create: (_) => User()),
],
child: MaterialApp(
title: 'Flutter Demo',
home: HomePage(),
));
}
}
使用 MultiProvider 包装您的 MaterialApp。 我可能在上面的代码中出现了一些括号问题
尝试用 Consumer 包装需要从提供者获取日期的小部件:
Foo(
child: Consumer<Auth, User>(
builder: (context, auth, user, child) {
return YourWidget(a: yourProviderData, child: child);
},
child: Baz(),
),
)
您也可以尝试使用小部件 Builder(builder: (context) => )
包装您的代码,以访问最近的上下文引用。
回答我的可见性问题。所以在这里找到了我的问题的答案。 https://github.com/rrousselGit/provider/issues/422#issuecomment-632030627
基本上我以错误的方式导入了包,我做到了
导入 'path/file.dart'
而不是指定完整路径
导入 'package:/path/file.dart'
嗨,我是 Flutter 和 Provider 的新手,无法理解这个错误。基本上,我使用 MultiProvider
来管理如下状态,这对其中一个(Auth)非常有用,但对其他(User)却不起作用,即使我以相同的方式使用它们。
我收到这个错误。
Note there are actually more providers, but simplified it for sake of simpler code example
错误
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following ProviderNotFoundException was thrown building StartPage(dirty, dependencies: [_InheritedProviderScope<Auth>, _InheritedProviderScope<UserLocation>, _InheritedProviderScope<UserState>, _InheritedProviderScope<BottomNavigationBarProvider>]):
Error: Could not find the correct Provider<User> above this StartPage Widget
To fix, please:
* Ensure the Provider<User> is an ancestor to this StartPage Widget
* Provide types to Provider<User>
* Provide types to Consumer<User>
* Provide types to Provider.of<User>()
* Ensure the correct `context` is being used.
If none of these solutions work, please file a bug at:
Main.dart
class SchoolApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Auth()),
ChangeNotifierProvider(create: (_) => User()),
],
child: HomePage(),
));
}
}
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
UserState _userState = Provider.of<UserState>(context);
switch (_userState.status) {
case UserStatus.UnAuthenticated:
return LoginScreen();
case UserStatus.Authenticated:
return StartPage();
}
}
}
StartPage.dart
class StartPage extends StatelessWidget with ChangeNotifier {
Timer timer;
@override
Widget build(BuildContext context) {
final _auth = Provider.of<Auth>(context);
final _user = Provider.of<User>(context, listen: true);
...
User.dart
class User extends ChangeNotifier{
Firestore db = Firestore.instance;
String _userId, _firstName, _lastName, _school, _email, _description, _employer, _title, _name;
List<String> _ideologies, _interests, _religions;
UserType _userType;
...
Auth.dart
class Auth extends ChangeNotifier {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signInWithEmailAndPassword(String email, String password) async {
final AuthResult authResult = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
notifyListeners();
return authResult.user.uid.toString();
}
...
class SchoolApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Auth>(create: (_) => Auth()),
ChangeNotifierProvider<User>(create: (_) => User()),
],
child: MaterialApp(
title: 'Flutter Demo',
home: HomePage(),
));
}
}
使用 MultiProvider 包装您的 MaterialApp。 我可能在上面的代码中出现了一些括号问题
尝试用 Consumer 包装需要从提供者获取日期的小部件:
Foo(
child: Consumer<Auth, User>(
builder: (context, auth, user, child) {
return YourWidget(a: yourProviderData, child: child);
},
child: Baz(),
),
)
您也可以尝试使用小部件 Builder(builder: (context) => )
包装您的代码,以访问最近的上下文引用。
回答我的可见性问题。所以在这里找到了我的问题的答案。 https://github.com/rrousselGit/provider/issues/422#issuecomment-632030627
基本上我以错误的方式导入了包,我做到了 导入 'path/file.dart'
而不是指定完整路径 导入 'package:/path/file.dart'