将 Firebase 云消息传递数据传递到当前屏幕上方的 n 个屏幕以及 Flutter 中的整个应用程序 [英] Passing Firebase Cloud Messaging data to n screens above the current screen and across the app in Flutter

查看:15
本文介绍了将 Firebase 云消息传递数据传递到当前屏幕上方的 n 个屏幕以及 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 是 Fl​​utter 自带的基本解决方案.您通常会有一个带有 StateStatefulWidget,它用 InheritedWidget 包装您的 MaterialApp 以广播 状态到你的屏幕.

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 云消息传递数据传递到当前屏幕上方的 n 个屏幕以及 Flutter 中的整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆