Refresh Flutter Text widget content every 5 minutes or periodically

前端 未结 1 1648
不知归路
不知归路 2021-02-09 04:34

I have a Flutter Text widget and its content is populated from an external REST call.I would like to refresh the widget content periodically every 5 mins by calling

相关标签:
1条回答
  • 2021-02-09 05:02

    Replace this :

    new Timer.periodic(oneSecond, (Timer t) => buildCountWidget());
    

    By this:

    new Timer.periodic(oneSecond, (Timer t) => setState((){}));
    

    And it should work, every time you call setState it'll refresh the widget and will call to the Future method again.

    UPDATE

    It's working fine, if you make these changes, you will notice how the data is refreshed (just for testing):

            Future<String> fetchPatientCount() async {
              print("fetchPatientCount");
              return DateTime.now().toIso8601String();
            }
    
            ...
    
            new FutureBuilder<String>(
                    future: fetchPatientCount(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.done) {
                        if (snapshot.hasData) {            
                          /* below text needs to be updated every 5 mins or so */
                          return new Text('#' + snapshot.data.toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.w700,
                                  fontSize:7.0));
                        } else if (snapshot.hasError) {
                          return new Text("${snapshot.error}");
                        }
                      }
    

    If the data changes every 25 seconds, it's working , you have to check your fetchPatientCount method. ( encode the data to json before send requestBody)

    0 讨论(0)
提交回复
热议问题