简体   繁体   中英

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. 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
A generic BloC Provider for your Flutter apps. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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