0

I've started using flutter_bloc package instead of redux to try it out, but I'm not entirely sure how I'm going to call flutter bloc events when receiving things from native (Android/iOS). It was easier with redux because in my parent MyApp widget of my main.dart file, I passed in the redux store to a custom class I created, and dispatched methods from the said class (called MethodChannelHandler).

main.dart:

void main() {
    runApp(new MyApp());
}
class MyApp extends StatefulWidget {
    @override
    State<StatefulWidget> createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {
    final Store<AppState> store = Store<AppState>(
      // ... redux stuff ...
    );

    @override
    void initState() {

        // sauce
        MethodChannelHandler(store);

        super.initState();
    }
}

methodChannelHandler.dart:

class MethodChannelHandler {
    Store<AppState> store;

    MethodChannelHandler(this.store) {
        methodChannel.setMethodCallHandler(_handleMethod);
    }

    // Handle method calls from native
    Future _handleMethod(MethodCall call) async {
        if (call.method == A_METHOD) {
            store.dispatch("something from native")
        }
    }
}

NOTE: I'm inept when it comes to programming vocabulary so please, if possible, please give me a small snippet of example code like I have or link me to some GitHub repo I can refer to instead of giving me a block of text I'm probably not going to understand.

Johnny Boy
  • 790
  • 3
  • 7
  • 18

1 Answers1

2

In very simple way it's look like this:

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<SomeBloc>(
      create: (_) {
        final bloc = SomeBloc(); //Create bloc

        MethodChannelHandler(bloc); //Add method handler

        return bloc;
      },
      lazy: false,
      child: Text("Content"),
    );
  }
}

class SomeBloc extends Bloc {
  SomeBloc() : super(SomeInitState());

  @override
  Stream mapEventToState(event) async* {
    if (event is SomeEvent) {
      //Handle SomeEvent
    }
  }
}

class MethodChannelHandler {
  final SomeBloc someBloc;

  MethodChannelHandler(this.someBloc) {
    methodChannel.setMethodCallHandler(_handleMethod);
  }

  // Handle method calls from native
  Future _handleMethod(MethodCall call) async {
    if (call.method == A_METHOD) {
      someBloc.add(SomeEvent("something from native"));
    }
  }
}
  • ooo hold on, do you have an example for when you're using MultiBlocProvider? Because the MultiBlocProvider has a create for each bloc. Does that mean i'll need multiple MethodChannelHandler s for each bloc? – Johnny Boy Mar 23 '21 at 15:59
  • @JohnnyBoy you need to listen messages from chanel in every blocs? What messages you want to listen? – Ivan Krivtsov Mar 23 '21 at 16:03
  • I'm making a music app and I'm thinking it'll need at least 2 blocs listening (possibly more), each sending a different type of data: from single strings for pausing/playing/rewinding songs, to a collection/array of objects that represent songs. I'm new to BLoCs so perhaps I'm just not architecting my project correctly (should I be trying to make a single BLoC purposed for all methodchannel interactions?). I'm also thinking maybe I could nest BLoCs that require the methodchannels and use the child's context to invoke context.read. ¯\_(ツ)_/¯ – Johnny Boy Mar 23 '21 at 16:21
  • 1
    I think you can use single bloc for all interactions, but send different event to bloc. – Ivan Krivtsov Mar 23 '21 at 16:31
  • Makes sense to me. Just to make sure we're on the same page, BlocM receives all methodchannel stuff at the same level at the MultiBlocProvider. BlocM then sends events to BlocA and BlocB. If this is correct, then my question is, is it possible to get reference to BlocA and BlocB within BlocM? And if so, how? – Johnny Boy Mar 23 '21 at 18:35
  • 1
    Yout can pass one bloc to another bloc and listen its states like in bloc Todoes example https://stackoverflow.com/a/62785980/15446321 – Ivan Krivtsov Mar 24 '21 at 01:21
  • I cannot thank you enough for the lengths you're going to help out a beginner like myself. But seriously, thank you very much! – Johnny Boy Mar 24 '21 at 02:43
  • You can up some of useful comments, that's the best thanks. And you are welcome. – Ivan Krivtsov Mar 24 '21 at 03:52