Firestore fails to retrieve data after reconnecting to the Internet

后端 未结 4 699
臣服心动
臣服心动 2021-02-02 12:44

I\'m using Firestore with the Android SDK (11.6.2) and I\'m hitting an exception when my device was offline and reconnects to the Internet.

When requesting a document, f

4条回答
  •  梦如初夏
    2021-02-02 13:12

    I also have the same problem. There is workaround - application waits some time and makes another attempt to get the data from server.

    Here is mockup (this is Flutter/dart):

       List items;
    
       Widget build(BuildContext context) {
        ...
        loadData();
        ...
        //building th UI with data from items list
        ...
       }
    
    
        loadData() async {        
            //is not loaded but there is internet connection
            if (!isLoaded&&isConnected) {
              try {
                  querySnapshot = await query.getDocuments(source: Source.server);
                } 
                catch (ex) {
                  print(ex);
                  Future.delayed(Duration(seconds: 10), () {
                  // setState to trigger build 10 seconds later to make one more attempt
                      setState(() {
                          isLoaded = false;
                      });
                  });
              return;
            }
        // here goes the code to handle the result 
        items =  querySnapshot.documents.map((s) => Course.fromDb(s)).toList();
        }
    

提交回复
热议问题