您好,我是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:
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();
}
}
}
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);
...
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;
...
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();
}
...
答案 0 :(得分:0)
std::range::begin
用MultiProvider包装您的MaterialApp。 我可能在上面的代码中出现了一些括号问题
答案 1 :(得分:0)
尝试用Consumer包装需要从提供程序获取日期的小部件:
Foo(
child: Consumer<Auth, User>(
builder: (context, auth, user, child) {
return YourWidget(a: yourProviderData, child: child);
},
child: Baz(),
),
)
您也可以尝试使用小部件Builder(builder: (context) => )
包装代码,以访问最近的上下文引用。
答案 2 :(得分:0)
回答我的问题以提高知名度。因此,在这里找到了我的问题的答案。 https://github.com/rrousselGit/provider/issues/422#issuecomment-632030627
基本上,我以错误的方式导入了软件包, 导入“ path / file.dart”
代替指定完整路径 导入'package:/path/file.dart'