简体   繁体   English

Flutter StreamBuilder 返回 null

[英]Flutter StreamBuilder returned null

@override
  Widget build(BuildContext context) {

    final user = Provider.of<User>(context);

    return StreamProvider<QuerySnapshot>.value(   // irrelevant (?)
      value: DatabaseService().users,
      child: StreamBuilder<UserData>(
        stream: DatabaseService(uid: user.uid).userData,
        builder: (context, snapshot) {
          UserData userData = snapshot.data;
          if (!snapshot.hasData) {
            return ProfileSetup();
          } else {
            return Scaffold(...

I want to check if the user has already set up his profile by checking if there is any data of the user in the Firestore Cloud.我想通过检查 Firestore 云中是否有用户的任何数据来检查用户是否已经设置了他的个人资料。 Otherwise the user gets send to the setup page.否则,用户将被发送到设置页面。 The problem is that when there is finally the user data in the cloud I get an error message and a red screen for half a second, but after that it continues as it's suppost to.问题是,当云中最终有用户数据时,我会收到一条错误消息和半秒钟的红屏,但之后它会继续按预期进行。 Yet I don't understand why there is an error message.但是我不明白为什么会出现错误消息。

A build function returned null.构建函数返回 null。 The relevant error-causing widget was StreamBuilder< UserData>相关的导致错误的小部件是 StreamBuilder< UserData>

Use connectionstates in streambuilders.在流构建器中使用连接状态。 Try this:尝试这个:

@override
Widget build(BuildContext context) {

  final user = Provider.of<User>(context);

return StreamProvider<QuerySnapshot>.value(   // irrelevant (?)
   value: DatabaseService().users,
   child: StreamBuilder<UserData>(
   stream: DatabaseService(uid: user.uid).userData,
      builder: (context, snapshot) {
       UserData userData = snapshot.data;
       if (snapshot.hasData) {
       switch (snapshot.connectionState) {
       case ConnectionState.none:
                 return Text("No Connections");
       case ConnectionState.waiting:
                return CircularProgressIndicator();
       case ConnectionState.active:
       case ConnectionState.done:
         return snapshot.data.length > 0 ? ScaffoldPage() :ProfileSetup();
    default:
    break;
  }
  } 
          return Text("");

I am using firestore in my new project and has same problem我在我的新项目中使用firestore并且有同样的问题

In my case I wasn't getting any error but snapshot has no data so I did checked就我而言,我没有收到任何错误,但快照没有数据,所以我检查了

if (snapshot.hasError) return Text('error ${snapshot.error}');

which helped me see what's error and it was because index wasn't enable这帮助我看到了错误,这是因为未启用索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM