简体   繁体   English

如何在 Flutter 中开发通用的 bloc 和 bloc provider?

[英]How to develop a generic bloc and bloc provider in Flutter?

In my flutter application there are so many pages and in most of the pages I send post requests to fetch a list of Objects and I have to use a lot of bloc and bloc provider classes to do so.在我的 flutter 应用程序中有很多页面,在大多数页面中,我发送 post 请求以获取对象列表,我必须使用大量 bloc 和 bloc 提供程序类来执行此操作。 Is there any way to develop a generic bloc provider with the generic bloc, so that I can use it in all of my pages?有没有办法用通用块开发通用块提供程序,以便我可以在所有页面中使用它?

You can directly use or reference source code of the following two package可以直接使用或参考以下两个包的源码
https://pub.dev/packages/generic_bloc_provider https://pub.dev/packages/generic_bloc_provider
A generic BloC Provider for your Flutter apps. Flutter 应用程序的通用 BloC 提供程序。 code snippet代码片段

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: AppBloc(),
      child: MaterialApp(
        title: 'Yo Sleep',
        home: MainPage(),
        initialRoute: 'main',
        routes: {
          'main': (context) => MainPage(),
        },
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appBloc = BlocProvider.of<AppBloc>(context);
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Header(),
            RecordList(appBloc),
          ],
        ),
      ),
    );
  }
}

https://pub.dev/packages/flutter_bloc A Flutter package that helps implement the BLoC pattern. https://pub.dev/packages/flutter_bloc帮助实现 BLoC 模式的 Flutter 包。

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

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