[英]Broadcast data to multiple widgets in Flutter
Is there a way to broadcast data from one widget to other widgets? 有没有办法将数据从一个小部件广播到其他小部件? Similar to a BroadcastReceiver on Android or NSNotificationCenter on iOS. 类似于Android上的BroadcastReceiver或iOS上的NSNotificationCenter。
Specifically, I'm trying to detect when a Navigator pops or pushes a new view. 具体来说,我试图检测导航器何时弹出或推送新视图。 I added navigatorObservers to MaterialApp. 我将navigatorObservers添加到MaterialApp。 And when a widget comes back to the foreground, I want to be able to notify it by broadcasting changes from didPush and didPop with the actual route that's in the current foreground 当一个小部件回到前台时,我希望能够通过广播didPush和didPop的变化来通知它,当前前景中的实际路由
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
). Navigator.push
返回一个将在调用Navigator.pop
时完成的Future
(这可以选择用于将数据传递回调用push
的小部件)。 So for example suppose your login button has this onPressed
handler: 例如,假设您的登录按钮具有此onPressed
处理程序:
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. 当您的登录页面调用Navigator.pop(true)
, Future
将以值true
完成,该值将分配给isLoggedIn
变量。 (You'll get null
if the user uses the back button to return to the previous screen.) (如果用户使用后退按钮返回上一个屏幕,您将获得null
。)
This is how showDialog
works as well. 这就是showDialog
工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.