1

In my app i have screens that fetched data from the Api, and sometimes the backend can update or change something on the api, so if the user is using the app, and the user presses the home button (middle android button. the circle) and comes back to the app later, i dont see the data being updated so no api call is being made, but when i press the back button and come back to the app the api call is being made same as when i close the app and remove it from the opened app history and open it again, the api call is being made, so i need that to happened the same when the home button is pressed, the user gets out the app by pressing the home button, comes back and the api call will be made. I looked in to it and i was told i need to use WidgetsBindingObserver class. Any help would be appreciated, let me know if you need any code to post.

i tried this but it wont work


class _StopWorkingScreenState extends State<StopWorkingScreen>
    with WidgetsBindingObserver {
  late Future<Response> futureDataForStatus;

  @override
  void initState() {
    super.initState();
    futureDataForStatus = getLocationStatus();
    WidgetsBinding.instance!.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        print("Inactive");
        break;
      case AppLifecycleState.paused:
        print("Paused");
        break;
      case AppLifecycleState.resumed:
        futureDataForStatus;
        break;
    }
  }
LearnFlutter
  • 214
  • 2
  • 22

1 Answers1

2

You need to call the function in your switch operator, not just pass a variable.

You could do something like:

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    ...
    case AppLifecycleState.resumed:
      setState(() {
        futureDataForStatus = getLocationStatus();
      });
      break;
    }
}

This will update the state on opening of the app and reload the view with new data (assuming you are using futureDataForStatus as future of a FutureBuilder somewhere on the page.

Charles Rostaing
  • 548
  • 5
  • 17