flutter_foreground_task icon indicating copy to clipboard operation
flutter_foreground_task copied to clipboard

Send sendport to the foreground task

Open TheAlbertDev opened this issue 1 year ago • 2 comments

We have an application that uses the Drift database. Until now, we were working with version 6.2.1 of the Flutter Foreground Task and sending the sendport to the Drift isolate to the foreground so that the foreground could perform database operations.

Now, we have migrated to version 8.10.4 of the plugin, and we see that sendports can no longer be sent to the foreground. What is the new recommended way to proceed with the new version to send sendports to the foreground?

TheAlbertDev avatar Oct 11 '24 16:10 TheAlbertDev

Hello, @TheAlbertDev

You can send sendPort to UI in a new way.

This plugin supports two-way communication between TaskHandler and UI in new version.

Therefore, there is no need to use sendPort for communication. Please see the readme for more details.

https://github.com/Dev-hwang/flutter_foreground_task?tab=readme-ov-file#hatched_chick-deepening

// version 6.2.0
class FirstTaskHandler extends TaskHandler {
  @override
  void onStart(DateTime timestamp, SendPort? sendPort) async {
    // Send data to main isolate.
    final ReceivePort newReceivePort = ReceivePort();
    final SendPort newSendPort = newReceivePort.sendPort;
    sendPort?.send(newSendPort);
  }
}

class _ExamplePageState extends State<ExamplePage> {
  void _onReceiveTaskData(Object data) {
    print('onReceiveTaskData: $data');
  }

  @override
  void initState() {
    super.initState();
    
    final ReceivePort newReceivePort = FlutterForegroundTask.receivePort;
    newReceivePort?.listen(_onReceiveTaskData);
  }
}
// version 8.10.4
void main() {
  // Initialize port for communication between TaskHandler and UI.
  FlutterForegroundTask.initCommunicationPort();
  runApp(const ExampleApp());
}

class MyTaskHandler extends TaskHandler {
  @override
  Future<void> onStart(DateTime timestamp, TaskStarter starter) async {
    // Send data to main isolate.
    final ReceivePort newReceivePort = ReceivePort();
    final SendPort newSendPort = newReceivePort.sendPort;
    FlutterForegroundTask.sendDataToMain(newSendPort);
  }
}

class _ExamplePageState extends State<ExamplePage> {
  void _onReceiveTaskData(Object data) {
    print('onReceiveTaskData: $data');
  }

  @override
  void initState() {
    super.initState();
    // Add a callback to receive data sent from the TaskHandler.
    FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);
  }

  @override
  void dispose() {
    // Remove a callback to receive data sent from the TaskHandler.
    FlutterForegroundTask.removeTaskDataCallback(_onReceiveTaskData);
    super.dispose();
  }
}

Dev-hwang avatar Oct 12 '24 07:10 Dev-hwang

Hello @Dev-hwang, and thank you for your response. What we want to do is send to the TaskHandler the sendport offered by drift for its own isolate, so that the TaskHandler can directly save data collected from a Bluetooth device into the database. The application is designed to operate in the background, meaning without the app being active or with the phone locked, so I’m not sure if sending the data to the UI is a viable option.

TheAlbertDev avatar Oct 13 '24 11:10 TheAlbertDev

Hello @Dev-hwang, and thank you for your response. What we want to do is send to the TaskHandler the sendport offered by drift for its own isolate, so that the TaskHandler can directly save data collected from a Bluetooth device into the database. The application is designed to operate in the background, meaning without the app being active or with the phone locked, so I’m not sure if sending the data to the UI is a viable option.

Have you managed to solve this issue? I'm banging my head now at exactly the same problem.

Tumist76 avatar Jan 14 '25 05:01 Tumist76