找不到正确的提供者不一致的行为
Posted
技术标签:
【中文标题】找不到正确的提供者不一致的行为【英文标题】:Could not find the correct Provider inconsistent behaviour 【发布时间】:2020-09-10 09:20:10 【问题描述】:您好,我是 Flutter 和 Provider 的新手,无法理解这个错误。基本上我使用MultiProvider
来管理如下状态,即使我以相同的方式使用它们,这对其中一个(Auth)但对其他(用户)来说非常有用。
我得到这个错误。
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);
...
用户.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();
...
【问题讨论】:
【参考方案1】: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。 我可能在上面的代码中提出了一些括号问题
【讨论】:
这也不起作用,我遇到了同样的错误,但无论如何我都不应该把它放在我的 MaterialApp 之上,因为理想情况下你想把它放在最远的地方尽可能降低三个,对吗? 哦,我的错,我没有看到您没有定义类型 ChangeNotifierProvider,您可以在我的原始答案中看到更改。告诉我它是否仍然显示错误。【参考方案2】:尝试用 Consumer 包装需要从提供者获取日期的小部件:
Foo(
child: Consumer<Auth, User>(
builder: (context, auth, user, child)
return YourWidget(a: yourProviderData, child: child);
,
child: Baz(),
),
)
您也可以尝试使用小部件Builder(builder: (context) => )
包装您的代码,以访问最近的上下文引用。
【讨论】:
这对我不起作用,同样的错误。如果我从这篇文章中正确理解 ***.com/questions/58774301/… 消费者在后台使用 provider.of【参考方案3】:回答我的可见性问题。所以在这里找到了我的问题的答案。 https://github.com/rrousselGit/provider/issues/422#issuecomment-632030627
基本上我以错误的方式导入包,我做到了 导入'路径/file.dart'
而不是指定完整路径 导入'package:/path/file.dart'
【讨论】:
以上是关于找不到正确的提供者不一致的行为的主要内容,如果未能解决你的问题,请参考以下文章