将Firebase Cloud Messaging数据传递到当前屏幕上方的n个屏幕以及Flutter中的整个应用程序 [英] Passing Firebase Cloud Messaging data to n screens above the current screen and across the app in Flutter
问题描述
我在Flutter中有一个应用程序可以在父屏幕中提取大量数据.父屏幕分为几个子屏幕,而子屏幕又进入了几个子屏幕.
I've got an app in Flutter that's pulling a lot of data in the parent screen. The parent screen gets divided into a few child screens and those go to a few more child screens.
现在,为了确保这不是一直在从API中提取数据,我只在根屏幕中将其提取一次-90%的时间是可以的.
Now, in order to make sure this isn't pulling data all the time from the API, I'm only pulling it once in the root screen - which is fine 90% of the time.
所有后续更新都会通过Firebase云消息传递在整个应用程序中广播
All the subsequent updates are broadcasted throughout the app via firebase cloud messaging
如果我在屏幕3上,我需要更新屏幕1上的数据,该数据也应该更新最终将在屏幕2上显示的数据.我目前正在使用此方法来传递我的数据( https://flutter.io/cookbook/navigation/passing-data/)
If I'm on screen 3, I need to update data in screen 1 which should also update data which will eventually be displayed on screen 2. I'm currently using this method to pass my data around (https://flutter.io/cookbook/navigation/passing-data/)
例如,屏幕1包含三个项目.每个项目都有自己的屏幕.
For example, screen 1 contains three projects. Each project has its own screen.
屏幕2.1用于项目1,屏幕2.2用于项目2,依此类推.
Screen 2.1 is for project 1, screen 2.2 is for project 2 and so on.
在屏幕2.1中,有n个待办事项列表.
In screen 2.1, there are n to do lists.
现在,如果我打开待办事项列表,则进入屏幕3,其中显示了项目1的第一个待办事项列表.
Now if I open the to do list, I reach screen 3 with the data of the first to do list of project 1.
在firebase云消息传递数据通知中,我收到了此待办事项列表的新信息以及项目2待办事项列表1的待办事项列表中的某些内容.
In a firebase cloud messaging data notification, I receive the new information of this todo list and something from the todo list of project 2 todo list 1.
如何保持一致性并全面更新数据?
How do I maintain consistency and update the data across the board?
我是否需要更改架构并使用Redux或类似的东西?
Do I need to change my architecture and use Redux or something along those lines?
推荐答案
很好的问题!
您将需要一个中央数据服务",作为单一事实来源.
You will need a central "data service" that acts as a single source of truth.
它将加载初始数据并将其存储在内部,还会在云消息到达时更新数据,并通知所有依赖于数据的小部件数据已更改.
It loads the initial data and stores it inside of it, it also updates the data when a cloud message arrives, and notifies all widgets that depend on the data that the data has changed.
屏幕从不存储数据的可变副本,而是查询数据服务以获取最新数据.
The screens never store a mutable copy of the data, instead they query the data service to get the latest data.
取决于应用程序的复杂性,有不同的解决方案可以使用流,也可以不使用流.
Depending on the complexity of your app, there are different solutions which work either with or without streams.
InheritedWidget
是Flutter随附的基本解决方案.通常,您会得到一个带有State
的StatefulWidget
,该MaterialApp
用InheritedWidget
包裹MaterialApp
以便将State
广播到屏幕上.
InheritedWidget
is a basic solution Flutter comes with. You would usually have a StatefulWidget
with a State
that wraps your MaterialApp
with an InheritedWidget
to broadcast the State
to your screens.
scoped_model是InheritedWidget
的薄包装,但是基本上提供了相同的功能.同样,模型提供者必须包装您的MaterialApp
,以使其在所有屏幕上都可用.
scoped_model is thin wrapper around InheritedWidget
, but basically offers the same feature. Again, the model provider must wrap your MaterialApp
to make it available on all screens.
BLoC和flutter_redux是更高级的解决方案.
BLoC and flutter_redux are more advanced solutions.
这篇关于将Firebase Cloud Messaging数据传递到当前屏幕上方的n个屏幕以及Flutter中的整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!