Home > Software engineering >  How to implement a communication chain in Flutter app with non-Flutter component
How to implement a communication chain in Flutter app with non-Flutter component

Time:10-06

I'm trying to implement a communication chain in my app. The app has at least to layer:

  • core, which is responsible for network communications. It's implemented as a Dart library
  • UI, which is responsible for communication with user. It's implemented as a Flutter app.

The core has a piece that handles invitations. Communication part is asynchronous and works in a way:

  • receive a request
  • handle request
  • send a response
 void _handleMemberInviteRequest(AtNotification notification) async {
    final sender = AtSignMember(atSign: notification.from);
    if (await onMemberInvitation(sender)) {
      send(notification.from, CommunicationVerbs.memberInviteRespond.name,
          "accept");
    } else {
      send(notification.from, CommunicationVerbs.memberInviteRespond.name,
          'reject');
    }
  }

onMemberInvitation is an event handler that in my understanding should be implemented in Flutter app. My problem is that I need user to accept an invitation. The whole chain of actions I see:

Request is received (core) -> onMemberInvitation is invoked (core) -> Confirmation dialog pops up (Flutter app) -> Response is returned by onMemberInvitation (core) -> Response is sent (core).

What I can't figure out is how to make Flutter to pop up the confirmation and answer with the result. I use BLoC patter for state management. So I though of having a separate Cubit that would emit a state that would be listened by a BlocListener on a top of application and pop up the dialog.

class Confirmation extends Cubit {
  void askForConfirmation(sender) {
    emit(ConfirmationState("InvitationConfirmation")); 
  }

  void gotConfirmation(bool confirmation) {
    emit(ConfirmationResponseState(confirmation));
  }
}

and in app initialization implement an onMemberInvitation handler:

Future<bool> onMemberInvitation(sender) async {
  askForConfirmation(sender);
  await for (/* here somehow wait for `ConfirmationResponseState`*/) {
    return confirmation;
  }
}

But then I can't realise how do I wait for the response in onMemberInvitation handler.

Any ideas? Can BLoC be utilised here as well? Or because it's outside of Flutter app some custom streams have to be implemented? Or there is another way?

CodePudding user response:

What you need is an async onMemberInvitation function that you can finish from outside the scope of the function itself.

You can achieve this using a Completer. This enables you to emit the result of the confirmation from anywhere while pausing the execution of onMemberInvitation until the result arrived. Check the sample below.

import 'dart:async';

Completer completer = new Completer<bool>();

void main() async {
  String sender = 'test';
  completer = new Completer();
  if (await onMemberInvitation(sender)) {
    print("accept");
  } else {
    print('reject');
  }
}

Future<bool> onMemberInvitation(String sender) async {
  askForConfirmation(sender);
  print('UI event emitted');

  return await completer.future;
}

void askForConfirmation(String sender) async {
  // TODO: emit the state to the UI here
  await Future.delayed(Duration(seconds: 3));

  //TODO: call this when you get the confirmation event
  gotConfirmation(true);
}

void gotConfirmation(bool confirmation) {
  completer.complete(confirmation);
}
  • Related