Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

flutter: inter-bloc communication, passing data events between different blocs

I haven't found much about inter-bloc communication, so I came up with an own, simple solution that might be helpful to others.

My problem was: for one screen I use 2 blocs for different information clusters, one of them also re-used on another screen. While passing data is well documented, I had issues with figuring out how to pass events or trigger states to/of the other bloc.

There are probably much better solutions, but for other flutter or bloc beginners like me it might be helpful. It is fairly simple and the logic is easy to follow.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

If you inject Bloc A as dependency to Bloc B (looked simple to me and I do not need further Blocs), I can get/set values in Bloc A from Bloc B (not vice versa). If I want to get data back to Bloc A, or if I just want the Bloc A build to reload, I can trigger events in the BlocBuilder of B to pass the information.

// ========= BLOC FILE ===========

class BlocA extends BlocAEvent, BlocAState> {
  int myAVar = 1;
}

class BlocB extends BlocBEvent, BlocBState> {
  BlocB({@required this.blocA}) : super(BInitial());
  final BlockA blockA;
  // passing data back and forth is straight forward
  final myBVar = blockA.myAVar + 1;
  blockA.myAVar = myBVar;

  @override
  Stream<BState> mapEventToState(BEvent event) async* {
    if (event is BInitRequested) {
      // trigger state change of Bloc B and request also reload of Bloc A with passed argument
      yield LgSubjectShowSingle(blocAReloadTrigger: true);
    }
  }
}

// ========= UI FILE ===========

class MyPage extends StatelessWidget {
  MyPage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // inject dependency of page on both Blocs: A & B
    return MultiBlocProvider(
        providers: [
          BlocProvider<BlocA>(
            create: (BuildContext context) =>
            BlocA().add(BlocAInit()),
          ),
          BlocProvider<BlocB>(
            create: (BuildContext context) =>
            BlocB(BlocA: BlocProvider.of<BlocA>(
                    context),).add(BInitRequested()),
          ),
        ],
        child: BlocBuilder<BlocB, BState>(
          builder: (context, state) {
            if (state is BShowData) {
              // If a reload of Bloc A is requested (we are building for Bloc B, here) this will trigger an event for state change of Bloc A
              if (state.triggerStmntReload) {
                BlocProvider.of<BlocA>(context).add(AReloadRequested());
              };
              return Text("abc");
            }
          }
        )
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...