Passing data to multiple widgets in Flutter

Is there a way to translate data from one widget to other widgets? Like BroadcastReceiver on Android or NSNotificationCenter on iOS.

In particular, I'm trying to detect when a Navigator appears or pops a new view. I added navigatorObservers in MaterialApp. And when the widget returns to the foreground, I want to be able to notify it by passing changes with didPush and didPop with the actual route, which is in the current foreground

+3
source share
1 answer

Navigator.pushreturns Futurewhich will be completed when called Navigator.pop(and this can be used to transfer data back to the widget called push). For example, suppose your login button has this handler onPressed:

onPressed: () async {
  bool isLoggedIn = await Navigator.pushNamed(context, '/login');
  if (isLoggedIn) {
    // do something
  }
}

When your login page invokes Navigator.pop(true), the value Futurewill be completed with the value truethat will be assigned to the variable isLoggedIn. (You will receive nullif the user uses the back button to return to the previous screen.)

This is how it works showDialog.

+5
source

Source: https://habr.com/ru/post/1678425/


All Articles