Broadcast data to multiple widgets in Flutter

前端 未结 1 453
青春惊慌失措
青春惊慌失措 2021-01-03 06:34

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

Specifically, I\'m tryin

相关标签:
1条回答
  • 2021-01-03 07:01

    Navigator.push returns a Future that will complete when Navigator.pop is called (and this can optionally be used to pass data back to the widget that called push). So for example suppose your login button has this onPressed handler:

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

    When your login page calls Navigator.pop(true), the Future will complete with a value of true which will be assigned to the isLoggedIn variable. (You'll get null if the user uses the back button to return to the previous screen.)

    This is how showDialog works as well.

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