StreamBuilder snapshot is always ConnectionState.waiting

前端 未结 5 2039
故里飘歌
故里飘歌 2021-02-19 12:12

I thought I understood StreamBuilders but I have some doubts that are puzzling me.

I thought that a ConnectionState.waiting means that the connection with the stream is

相关标签:
5条回答
  • 2021-02-19 12:28

    You need to add listen:false to your provider.

    0 讨论(0)
  • 2021-02-19 12:29

    This is likely to be your Firestore rules. If it's like this:

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth.uid != null;
        }
      }
    }
    

    then you'll have t be authenticated to grab the data. You can test this quickly (but not permanently) by allowing access to the DB from anyone ...

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    ... but please do change it to something more secure afterwards.

    0 讨论(0)
  • 2021-02-19 12:29

    Encountered the same issue. Checking the code, if you passed a not null data, the status will be set to waiting.

    If you want to make the status to be 'none', then you need to pass null first and pass the Stream to the StreamBuilder when the data is on fetching.

    0 讨论(0)
  • 2021-02-19 12:41

    I use a stream variable otherwise I would have the same error

     Stream<List<List<User>>> friendUser;
      @override
      Widget build(BuildContext context) {
        final
         db = Provider.of<FirestoreDatabase>(context, listen: false);
        friendUser = StreamZip([db.usersStream1(), db.usersStream2()]);
       return Scaffold(
       ...
          StreamBuilder<List<List<User>>>(
                          stream: friendUser,
                          //qui ont deja discuter
                          initialData: [],
                          builder: (context, snapshot) {...}),
    
       );
    
    }
    
    0 讨论(0)
  • 2021-02-19 12:46

    I suggest you drop the if/else condition and use a switch to check for the ConnectionState.

    switch(snapshot.connectionState){
      case ConnectionState.waiting:
        return Center(child:CircularProgressIndicator());
        break;
      default:
        if(snapshot.hasError){
           // Handle errors
         }else if(!snapshot.hasData){
           // Handle no data
          }else {
           // Check for null
           var result = snapshot.data
           if(result!=null){
             //Handle the null value
           } else {
    
            }
          }
    
        break;
    
    
    }
    
    0 讨论(0)
提交回复
热议问题