【问题标题】:The argument type 'Stream<User?>' can't be assigned to the parameter type 'Stream<User>?'参数类型“Stream<User?>”不能分配给参数类型“Stream<User>?”
【发布时间】:2021-09-04 04:21:16
【问题描述】:

我想通过 Streams 使用 firebase 身份验证,但是我在stream: FirebaseAuth.instance.authStateChanges(), 线上遇到了上述错误。我试过 onAuthStateChange() 也没有用。

class LandingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget build(BuildContext context) {
      return StreamBuilder<User>(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
          if(snapshot.hasData) {
            print("data exists");
            return First();
          }
          else {
            return SignIn();
          }
        },
      );
    }
  }
}

【问题讨论】:

    标签: android firebase flutter firebase-authentication


    【解决方案1】:

    如果您查看authStateChanges 的文档,您会发现它返回一个Stream&lt;User?&gt;。所以你的StreamBuilder 也应该是User? 而不是User

    return StreamBuilder<User?>(
      ...
    

    【讨论】:

    • 非常感谢,这是一个完美的答案。
    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 2021-09-26
    • 2023-03-13
    • 1970-01-01
    • 2021-10-28
    • 2021-08-02
    • 2021-12-30
    • 2021-10-19
    相关资源
    最近更新 更多