0

Here is my code:

@override
  void initState() {
    super.initState();
    weatherFuture = _getWeather();
  }

  @override
  Widget build(BuildContext context) {
    double viewBoxWidth = MediaQuery.of(context).size.width;

    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FutureBuilder(
              future: weatherFuture,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Container(
                    width: viewBoxWidth * 0.35,
                    color: Colors.orange, // Todo: Remove this color later
                    child: WeatherTileColumn(
                      topTileDisplayText: snapshot.data.areaName,
                      bottomTileDisplayText: snapshot.data.weatherDescription,
                      topTileIcon: '',
                      bottomTileIcon:
                          _getWeatherIcon(snapshot.data.weatherConditionCode),
                    ),
                  );
                } else {
                  return Text(
                    'Loading Data...',
                    style: TextStyle(fontFamily: 'Merriweather', fontSize: 15),
                    textAlign: TextAlign.center,
                  );
                }
              }),
          Container(
            width: viewBoxWidth * 0.2,
            margin: EdgeInsets.only(top: 95),
            child: TextButton(
              child: Icon(
                Icons.refresh,
                color: Colors.black87,
                size: 55,
              ),
              style: ButtonStyle(
                  backgroundColor:
                      MaterialStateProperty.all(Colors.transparent)),
              onPressed: () {
                setState(() {
                  weatherFuture = _getWeather();
                });
              },
            ),
          ),
          FutureBuilder(
            future: weatherFuture,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Container(
                  width: viewBoxWidth * 0.35,
                  color: Colors.purple,
                  child: WeatherTileColumn(
                    topTileDisplayText: snapshot.data.temperature.toString(),
                    bottomTileDisplayText:
                        snapshot.data.tempFeelsLike.toString(),
                    topTileIcon: '',
                    bottomTileIcon: '‍️',
                  ),
                );
              } else {
                return Text(
                  'Loading Data...',
                  style: TextStyle(fontFamily: 'Merriweather', fontSize: 15),
                  textAlign: TextAlign.center,
                );
              }
            },
          )
        ],
      ),
);

In this code, if you look at the main widgets inside the children of the Row widget, you will see that there are two FutureBuilder widgets and a Container widget.

What has to happen is upon pressing that TextButton inside the Container, I want the two future builder widgets to be re updated with newly fetched data.

My current attempt was to do:

setState() {
  weatherFuture = _getWeather();
}

inside the callback function for the onPressed method. And it does not work. I looked at many solutions and articles and could not get this to work.

(Also I checked Reload data when using FutureBuilder stack overflow post and this is not a duplicate. In that post, the button that rebuilds the FutureBuilder widget is inside the FutureBuilder widget but in this case it isn't. So that solution did not work for me.)

halfer
  • 19,824
  • 17
  • 99
  • 186
  • @pskink thank you for the reply. I am a beginner. This is a bit vague for me. If it is not a bother can you kindly explain what is meant by using the key: UniqueKey() ? Thanks – NewPythoneer Aug 29 '21 at 10:45
  • so what does not work actually? `build` method is called but `FutureBuilder` does not "see" the new `Future`? – pskink Aug 29 '21 at 10:45
  • @pskink When the TextButton is pressed. I am expecting the data that is obtained asynchronously inside the FutureBuilder widget to be refetched, and the ui to be updated with the newly fetched data. It does not work.... – NewPythoneer Aug 29 '21 at 10:47
  • what do you see if you add `print(snapshot);` before `if (snapshot.hasData) {...`?, also add `print("onPressed")` inside `onPressed` callback – pskink Aug 29 '21 at 10:49
  • @pskink If I do a print(snapshot), it displays the weather data that I am fetching. But it is the same snapshot. The time does not change, which means the snapshot is not being update. – NewPythoneer Aug 29 '21 at 10:54
  • so your `_getWeather` method is broken then – pskink Aug 29 '21 at 10:55
  • @pskink Thanks for pointing this out.... Sounds like I will be needing to use a different API then..... Thanks.... – NewPythoneer Aug 29 '21 at 10:58
  • Try to call `setState(() {});` – stacktrace2234 Aug 29 '21 at 11:49
  • If you are an intern, then you can call on your colleagues to ask for help, assuming that your employer is at least a moderately good one. It is also fine to ask for help here, but go easy on the commentary about your circumstances - questions here are for the long term, for a wide future audience, and much of the meta-stuff isn't useful for them. How urgent things are for you is also immaterial, especially given that the readership are nearly all volunteers. – halfer Aug 29 '21 at 18:59

0 Answers0