如何在Flutter中创建服务/提供程序? [英] How do you create Services/Providers in Flutter?

查看:330
本文介绍了如何在Flutter中创建服务/提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我需要一个具有所有用户变量和功能的用户服务,以及另一个具有所有项目变量和功能等的项服务。我希望所有这些数据和功能在整个项目中都可用,并将用户数据传递到例如,它是必需的。

Let's say I need a user service which has all my user variables and functions, and another item service with all item variables and functions, etc etc. I want all this data and functions available throughout the project, passing the user data where it's needed, for example.

我怀疑它与继承的小部件有关,但是这如何工作?就像,我知道如何在根级别使用一个继承的小部件,但是我应该为每个服务在根级别构建一堆继承的小部件吗?还是只将所有数据放在一个继承的顶级小部件中?似乎可能会变得凌乱。我还没有看到这样的例子。

I suspect it has something to do with inherited widgets, but how does that work? Like, I see how I could use one inherited widget at the root level, but am I supposed to build a bunch of inherited widgets at the root-level for each service? Or just put ALL the data in the one top inherited widget? Seems like that may get messy. I have yet to see an example of this.

...还是应该只使用带有静态变量的类并在需要的地方调用它?

... or should I just be using a class with static variables and call on that where I need it?

推荐答案

请参见上面Günter提供的链接。如果您有三十分钟的时间观看Brian Egan的DartConf 18演示文稿:保持简单,状态。另请参见此处的示例代码。他展示了使用InheritedWidget和Controller以及Flutter Redux编码的相同应用。

See the link provided by Günter above. If you have thirty minutes watch Brian Egan's DartConf 18 presentation: Keep it Simple, State. Also see Brian's example code here. He shows the same app coded using InheritedWidget with a Controller, and with Flutter Redux.

这是我的一个简单应用中的单个InheritedWidget的示例...

Here's a example of a single InheritedWidget from a simple app of mine...

class StateContainer extends InheritedWidget {
  final List<Membership> memberships;

  final IntFunction getNextIndex;
  final VoidMembershipFunction updateMembership;
  final VoidIntFunction deleteMembership;

  const StateContainer({
    this.memberships,
    this.getNextIndex,
    this.updateMembership,
    this.deleteMembership,
    Widget child,
  })
      : super(child: child);

  static StateContainer of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(StateContainer);
  }

  @override
  bool updateShouldNotify(StateContainer oldWidget) {
    return true;
  }
}

我作为孩子传入了我的MaterialApp。然后,如果StateContainer可以访问数据和方法,则很大一部分树将变为无状态。您可以将它们视为模型和控制器合二为一。树中的构建方法通常以...

I pass in my MaterialApp as the child. A high proportion of the tree then becomes stateless as it has access to the data and methods if the StateContainer. You could think of these as your model and controller in one. Build methods in the tree often start with...

  @override
  Widget build(BuildContext context) {
    final StateContainer container = StateContainer.of(context);

以访问它们。

没错,单个InheritedWidget很快就变得笨拙-您可以在其中投资探索Redux-请参阅讲座的最后10分钟。以我的经验,最混乱的地方是在比较所有成员变量时的updateShouldNotify方法。 (在这个简单的示例中,只有一个成员变量,并且setState仅在其更改时才被调用,因此它总是正确的。)

You are right that a single InheritedWidget becomes unwieldy quickly - which is where you might invest in exploring Redux - see the last 10 minutes of the talk. In my experience, the area that gets messiest quickest is the updateShouldNotify method when ends up comparing all the member variables. (In this simple example there is only one member variable, and setState only gets called when it changes, so it's trivially always true.)

这篇关于如何在Flutter中创建服务/提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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