简体   繁体   English

Flutter-如何从另一个小部件重建将来的小部件

[英]Flutter - How to rebuild a future widget from another widget

I tried to make the code below simple as possible, I have no idea what approach I should be trying to take. 我试图使下面的代码尽可能简单,但我不知道我应该尝试采用哪种方法。

I have a future builder that returns weather API data. 我有一个将来的构建器,可以返回天气API数据。 I have a timer to stop spamming of API. 我有一个计时器来停止向API发送垃圾邮件。 But how can I get the weather widget to reload so the FutureBuilder is called again? 但是,如何才能重新加载天气小部件,以便再次调用FutureBuilder?

main.dart main.dart

  CurrentWeatherWidget(),

current_weather_widget.dart current_weather_widget.dart

  return Column(
  children: <Widget>[
    FutureBuilder<CurrentWeatherFactory>(
        future: main,
        builder: (context, snapshot) {
          if (snapshot.hasData)
          {
           >> DISPLAY WIDGET
          } else if (snapshot.hasError)
          {
            return Text(snapshot.error);
          }
CircularProgressIndicator(),

some_other_widget.dart some_other_widget.dart

RaisedButton(
onPressed: (){
    if (timer() == 0){
        timerReset();
        >> RELOAD current_weather_.dart

    } else {
        print("no new data");
    }

    },
    child: Text('RELOAD NEW DATA'),

),

Any help is appreciated. 任何帮助表示赞赏。

I would suggest replacing the FutureBuilder with a StreamBuilder and updating the stream every time new weather data is available. 我建议每次使用新的天气数据时,用StreamBuilder替换FutureBuilder并更新流。 So instead of using a Future you could use a BehaviorSubject and call its add method to update the stream. 因此,可以使用BehaviorSubject并调用其add方法来更新流,而不是使用Future。

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

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