使集团实例跨flutter应用程序进行全局,并以依赖方式注入时尚方式访问它们

发布于 2025-02-12 19:46:48 字数 712 浏览 0 评论 0原文

我有一些数据源在我的Flutter应用程序中传递的多个模型对象,每个数据源都由其ID(整数字段)识别。

每个模型及其状态可以在多个小部件中表示。

我正在为每个模型对象制作一个bloc对象(例如,中显示的3个模型 ,它们应该共享相同的bloc实例< /strong>)。

从其中一些小部件中,我可以将事件发送到BLOC实例,并具有新状态,当然,该状态应在共享BLOC实例的所有小部件中表示。

在获取数据时,我脑海中的情况应如下: 1-从某些数据源获取并将其传递到演示文稿的每个模型对象应搜索其相应的BLOC实例。 2-如果创建了BLOC实例,请使用此BLOC实例(并在必要时更新其状态)。 3-如果未创建BLOC实例,请创建一个并使用它。

我的主要问题是,我似乎无法创建这些集团实例在全球范围内,并以“注入依赖”方式访问它们。依赖性是集体实例,并由模型ID识别。

我通常使用get_it注册单身实例,但是现在我需要“ singleton实例由模型IDS唯一识别

,因为Blocs op opers opers oble opers opers opers oble streams opers of streams,所以我认为我们认为我们无法在我的情况下,请使用BlocProvider小部件。如果有解决方案,那也将不胜感激。

I have multiple model objects delivered by some data source in my Flutter app, each is identified by its ID (an integer field).

Each model and its state can be represented in multiple widget.

I'm making a bloc object for each model object (for example a model with ID of 3 shown in multiple widgets and they should share the same bloc instance).

From some of these widgets, I can send an event to the bloc instance, and have a new state, and of course, this state should be represented in all of the widgets that are sharing the bloc instance.

The scenario in my mind should be as follows when fetching the data:
1- Each model object fetched from some data source and is delivered to the presentation should search for its corresponding bloc instance.
2- If the bloc instance is created, use this bloc instance (and update its state if necessary).
3- If the bloc instance is not created, create one and use it.

My main problem is, I can't seem to be able to create those bloc instances globally, and access them in a "dependency injected" way. The dependency is the bloc instance and identified by the model ID.

I normally use get_it to register singleton instances, but now I need "Singleton instances uniquely identified by the model IDs"

Also, since blocs opens streams, these should be closed by hand since I think we can't use BlocProvider widgets in my case. If there is a solution to that, that would be much appreciated too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文