找不到正确的提供者不一致行为

时间:2020-05-23 14:31:34

标签: flutter dart flutter-provider

您好,我是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();
  }
...

3 个答案:

答案 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'