fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation - How to communicate between the background task (isolate) and other widgets

jonathanMNg opened this issue · comments

  • I have read the README
  • I have done the setup for Android
  • I have done the setup for iOS
  • I have ran the sample app and it does not work there

Version
0.5.0

Describe the error
Hi,
I'm trying to execute a Bloc event with the workmanager callbackDispatcher().
However, because the function callbackDispatcher itself is an Isolate, it can't modify the variables of the main execution. My workaround is to use SendPort and ReceivePort.

In the main.dart:

@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    if(task == 'sleep-timer') {
      final SendPort? send = IsolateNameServer.lookupPortByName('sleep_timer_send_port');
      if (send != null) {
        send.send(inputData);
      }
    }
    return Future.value(true);
  });
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Workmanager().initialize(callbackDispatcher,isInDebugMode: true);
  runApp(const MyApp());
}

In the timer_bloc.dart:


 TimerBloc() : super(TimerInitial()) {

    on<Start>((event, emit) async {
      Workmanager().cancelByTag('sleep-timer-tag');
      final ReceivePort port = ReceivePort();
      _bindBackgroundIsolate(port);
      await Workmanager().registerOneOffTask(
          UniqueKey().toString(),
          'sleep-timer',
          tag: 'sleep-timer-tag',
          initialDelay: event.duration,
          inputData: {"executeTask": true},
          existingWorkPolicy: ExistingWorkPolicy.replace
      );
  });

  void _bindBackgroundIsolate(ReceivePort port) {
    final isSuccess = IsolateNameServer.registerPortWithName(port.sendPort, 'sleep_timer_send_port');
    if (!isSuccess) {
      _unbindBackgroundIsolate('sleep_timer_send_port');
      _bindBackgroundIsolate(port);
      return;
    }
    port.listen((dynamic data) async {
      // final isDone = data;
      if(data != null && data['executeTask']) {
        do_other_task();
      }
    });
  }
  void _unbindBackgroundIsolate(String name) {
    IsolateNameServer.removePortNameMapping(name);
  }
}

I think that adding this to the documentation would help others. FYI, I copied that code from this project flutter_downloader
Thanks,