【问题标题】:Could not find the correct Provider<FirebaseUser> above this HomePage Widget在此主页小部件上方找不到正确的 Provider<FirebaseUser>
【发布时间】:2020-06-07 00:14:48
【问题描述】:

我刚刚开始使用提供程序包重构我的应用程序结构。现在我得到了上面的错误,即使我在下一个 BuildContext 中的小部件树下调用提供程序。 知道什么可能导致错误吗?据我了解,错误消息中解释的常见错误情况不适用于我。 Widget tree

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider(create: (_) => FirebaseAuth.instance.onAuthStateChanged)
      ],
      child: MaterialApp(
          title: "My App",
          home: HomePage()),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return _getLandingPage(Provider.of<FirebaseUser>(context), context);
  }

  Widget _getLandingPage(FirebaseUser firebaseUser, context) {
    if (firebaseUser != null) {
        return CreateProfileFlow();

    } else {
      return PhoneNrInput();
    }
  }
}

错误:

Error: Could not find the correct Provider<FirebaseUser> above this HomePage Widget

This likely happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

- 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 HomePage is under your MultiProvider/Provider<FirebaseUser>.
  This usually happen when you are creating a provider and trying to read it immediatly.

  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
      builer: (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

【问题讨论】:

    标签: flutter dart provider state-management


    【解决方案1】:

    FirebaseAuth.instance.onAuthStateChangedStream,但您使用 Provider 而不是 StreamProvider 来公开值。

    因此,如果要读取该值,则有两种选择:

    • 使用StreamProvider 代替Provider

      StreamProvider(
        create: (_) => FirebaseAuth.instance.onAuthStateChanged,
      ),
      
    • 继续使用Provider,获取context.watch&lt;Stream&lt;FirebaseUser&gt;&gt;的流:

      StreamBuilder(
        stream: context.watch<Stream<FirebaseUser>>(),
        builder: (context, snapshot) {
          ...
        },
      );
      

    【讨论】:

      【解决方案2】:

      当状态发生变化时,使用 StreamBuilder 检查用户数据。

      class AuthenticationWrapper extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return StreamBuilder<User>(
            stream: FirebaseAuth.instance.authStateChanges(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.active) {
                User user = snapshot.data;
                if (user == null) {
                  return LoginPage();
                }
                return HomePage();
              } else {
                return Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              }
            },
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-22
        • 2021-11-24
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 2021-08-24
        • 2021-10-27
        • 2021-01-27
        相关资源
        最近更新 更多