Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

flutter - How to pass data down the tree using flutter_blocs?

I am quite stuck on this thing for the past few days.

So my use case here is,? A -> B -> C -> D

Update some data in A when there is some change in D. A, B, C, D are different screens.

Now I understand we can give Two providers to the D using MultiBlocProvide. but I am using the pushed name route,? Not sure how to communicate here using blocs.

routes: {
  Routes.ScreenA: (context) => _getScreenA(),
  Routes.ScreenB: (context) => _getScreenB(),
  Routes.ScreenC: (context) => _getScreenC(),
  Routes.ScreenD: (context) => _getScreenD(),
}

  _getScreenA() {
    return BlocProvider<ScreenABloc>(
        create: (context) =>
            ScreenABloc(),
        child: ScreenARoute());
  }

  _getScreenB() {
    return BlocProvider<ScreenBBloc>(
        create: (context) => ScreenBBloc(),
        child: ScreenBRoute());
  }

  _getScreenC() {
    return BlocProvider<ScreenCBloc>(
        create: (context) => ScreenCBloc(),
        child: ScreenCRoute());
  }

  _getScreenD() {
    return BlocProvider<ScreenDBloc>(
        create: (context) => ScreenDBloc(),
        child: ScreenDRoute());
  }

Also checked the example?of Todo's, but now sure how to plug it here.

question from:https://stackoverflow.com/questions/65829588/how-to-pass-data-down-the-tree-using-flutter-blocs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather than wrapping each route with BlocProvider wrap your MaterialApp on your main.dart file with MultiBlocProvider and then initialize all your required bloc classes there, They will be available everywhere on your application. If you are going to way define same bloc over different routes - there will be some data inconsistencies because you are not using same instance of a bloc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...