提供程序与Get_it [英] Provider vs. Get_it

查看:75
本文介绍了提供程序与Get_it的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索Flutter的依赖注入解决方案时,我发现了两个很棒的库: provider get_it .

Searching for Dependency Injection solutions for Flutter, I found two awesome libraries: provider and get_it.

据我所知, provider 具有更多样板,但它非常适合Flutter,允许 Consumer 重建 Widget的一部分树,一旦注入值更改.

As far as I can see, provider has more boilerplate, but it fits really nicely with Flutter, allowing Consumer to rebuild parts of the Widget tree, once an injected value change.

get_it 更直接,更易于使用并且不依赖Flutter,因此可以与任何Dart代码一起使用.

get_it on the other hand is more straightforward, easier to use, and not dependant on Flutter, so can be used with any Dart code.

它们之间是否还有其他区别和限制?我知道这有点自以为是,但是Flutter太新了,因此可以很好地注册公共利益,副作用和陷阱.

Are there any more differences and limitations between them? I know this is kinda opinionated, but Flutter is so new that it's good to register publicly benefits, side-effects and gotchas.

推荐答案

两者之间的主要区别是 provider 不是严格依赖注入.

The main difference between both is that provider is not strictly dependency injection.

通过使用小部件,提供程序也可以:

By using widgets, provider is also able to:

  • 提供者与Flutter devtool兼容
  • 知道何时无法访问变量 (作用域为树)
  • 知道何时创建和处置对象
  • 同步模型->模型和模型-> UI
  • 仅覆盖特定窗口小部件树的某些值
  • 自愿防止循环依赖

从长远来看,所有这些选项都是可选的,它们对您的应用程序的健康有好处.

All of these are, while optional, good for the health of your app in the long run.

它可以确保您始终保持最新状态,使意大利面条式代码"变得更难,并使您的不同元素更容易组合.

It ensures that you're always up to date, makes it harder to have "spaghetti code", and makes your different elements more composable.

这篇关于提供程序与Get_it的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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