简体   繁体   English

Flutter 最佳实践在屏幕之间共享数据

[英]Flutter best practices share data between screen

I'm new in flutter and I read lot of documentations on multiple subjets.我是 flutter 的新手,我阅读了很多关于多个 subjets 的文档。 One of them is sharing data between screens.其中之一是在屏幕之间共享数据。 I found a lot of solutions and as I'm creating a base project with lot of features, I want to know which one is the best我找到了很多解决方案,当我正在创建一个具有很多功能的基础项目时,我想知道哪个是最好的

Solution 1: In constructor解决方案1:在构造函数中

Basically, when we navigate, we send the data through the next constructor widget.基本上,当我们导航时,我们通过下一个构造函数小部件发送数据。

Solution 2: In session解决方案 2:在 session

Creates a singleton which can be access from everywhere in the application and each time you need to send data, you add it in the session and the next widget can retrieve it.创建一个 singleton 可以从应用程序中的任何地方访问,每次需要发送数据时,将其添加到 session 中,下一个小部件可以检索它。

Solution 3: In blocs解决方案3:在集团

I read this solution which looks good:我读了这个看起来不错的解决方案:

I made a BLoC container class where I've instantiated the BLoCs of the two screen.我制作了一个 BLoC 容器 class ,在其中实例化了两个屏幕的 BLoC。 Then here I set the reference from the BLoC A to a stream of the BLoC B where I want to send the data.然后在这里我将 BLoC A 的引用设置为我想要发送数据的 BLoC B 的 stream。 The BLoCs are still decoupled cause they don't know anything about each other, the BLoC A doesn't get passed through the constructor on the BLoC B and vice versa, but it just knows that it will receive some data on one of its streams. BLoC 仍然是解耦的,因为它们彼此一无所知,BLoC A 不会通过 BLoC B 上的构造函数传递,反之亦然,但它只知道它将在其流之一上接收一些数据.

UPDATED:更新:

Solution 4: Inherited widget解决方案 4:继承的小部件

With a static function like:与 static function 一样:

static InheritedDataProvider of(BuildContext context) => context.inheritFromWidgetOfExactType(InheritedDataProvider);
}

So you can access to the data initialized in the parent with something like: final data = InheritedDataProvider.of(context).data;因此,您可以通过以下方式访问在父级中初始化的数据: final data = InheritedDataProvider.of(context).data;

Maybe there are others solutions and I'll be glad to know them.也许还有其他解决方案,我很高兴知道它们。 Thanks谢谢

What I use:我用什么:

  1. Call next screen with Navigator.pushNamed(ctx, '/foo', arguments: bar) .使用Navigator.pushNamed(ctx, '/foo', arguments: bar)调用下一个屏幕。 Only include arguments if needed (eg for "update detail" screen).如果需要,仅包括arguments (例如“更新详细信息”屏幕)。
  2. Centralize all possible routes in the widget that contains MaterialApp .在包含MaterialApp的小部件中集中所有可能的路由。
  3. Pass a clone of the passed argument to the next screen (immutable data).将传递的参数的副本传递到下一个屏幕(不可变数据)。

In code:在代码中:

  ...
  MaterialApp(
    onGenerateRoute: (s) => MaterialPageRoute(builder: (ctx) => _route(s), settings: s),
  )
  ...

  Widget _route(RouteSettings route) {
    switch (route.name) {
      case '/signup':
        return SignupRoute();
      case '/vendor':
        return VendorRoute((route.arguments as Vendor)?.data?.clone());
      default:
        throw ('No match for ${route.name}');
    }
  }

The best way is passing a parameter to the constructor最好的方法是将参数传递给构造函数

Navigator.push(
                context,
                PageTransition(
                    type: PageTransitionType.fade,
                    child: LoginPage(
                      userModel: model,
                    )));

then:然后:

class LoginPage extends StatefulWidget {
  LoginPage({this.userModel});
  User userModel;
  @override
  _LoginPageState createState() => _LoginPageState(userModel: userModel);
  }  

 class _LoginPageState extends State with TickerProviderStateMixin {
  User userModel;
  _LoginPageState({this.userModel}); 

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM