flutterfirebasedartgoogle-cloud-firestorestream-builder

StreamBuilder don't reload automatically, need hot reload the app


I've seen multiple questions about this but nothing helped me.

I have a StreamBuilder that look to a variable in Firestore, and this variable is use for a second StreamBuilder to get some text with an index.

When i update the variable on Firestore, the display of the variable is refreshed, but the texte is not, I need to hot reload or use the setstate() function.

I try to make a StreamSubscription for listening if the variable is update and call the setstate but that didn't work. I try calling setState in the StreamBuilder but I can't and even with WidgetsBinding.instance.addPostFrameCallback((timeStamp) { }).

Here my codes :

Stream for getting the variable

Stream _getIdDay() {
var stream = FirebaseFirestore.instance
    .collection('Others')
    .doc('Variables')
    .snapshots();
return stream; 
}

Streambuilder for showing the variable

StreamBuilder(
    stream: _getIdDay(),
    builder: (context, snapshot) {
         if (snapshot.connectionState == ConnectionState.active) {
             _idDay = snapshot.data['id_day'];
             return Text("$_idDay");
         } else {
             return Text("$_idDay");
         }
    },
),

Stream for getting the text

Stream _getQuestionAndDate(int idDay) {
    var date = FirebaseFirestore.instance
        .collection('Questions')
        .doc("$idDay")
        .snapshots();
    return date;
}

StreamBuilder for showing the text

StreamBuilder(
    stream: _getQuestionAndDate(_idDay),
    builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
            _question = snapshot.data['Question'];
            return Text(_question);
        } else {
            return Text(_question);
        }
    },
),

Solution

  • I think I managed to solve the problem.

    Since I need to call the setState function or hot reload the app for my StreamBuilder to get the new text, I put a ValueNotifier on my index variable, and on the initState, I create a listener so when the variable change, I call the setState function.

    I don't know if it's a good solution or no, but it's working, tell me if it's not and how I can improve that.

    My new code :

    final ValueNotifier<int> _idDay = ValueNotifier(1);
    
    
    @override
    void initState() {
    
      _idDay.addListener(() {
        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
          setState(() {});
        });
      });
    
      super.initState();
    }
    

    access to the value with _idDay.value