Dev-hwang / flutter_foreground_task

This plugin is used to implement a foreground service on the Android platform.
https://pub.dev/packages/flutter_foreground_task
MIT License
140 stars 105 forks source link

The parameter transfer of the new version of sendPort. send is restricted #214

Closed knight0zh closed 1 month ago

knight0zh commented 5 months ago

The parameter transfer of the new version of sendPort. send is restricted, and I am unable to implement the step counting function. Please tell me what to do?

$ flutter doctor                                                                                                                                                                                                                                   [13:58:10]
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel [user-branch], 3.13.9, on macOS 12.5 21G72 darwin-x64, locale zh-Hans-CN)
    ! Flutter version 3.13.9 on channel [user-branch] at /Users/knight0zh/software/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.85.1)
[✓] Connected device (4 available)
[✓] Network resources`
  ...
  @override
  void onRepeatEvent(DateTime timestamp, SendPort? sendPort) async {
    final pedometer = Pedometer.stepCountStream;
    final StepCount event = await pedometer.first;
    FlutterForegroundTask.updateService(
        notificationTitle: 'Current steps', notificationText: '${event.steps}');
    sendPort?.send(event);
  }
  ...

Error

E/flutter (14899): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: is a regular instance reachable via : Instance of 'StepCount'

Dev-hwang commented 1 month ago

The send function can only send primitive type, String, Collection provided by Flutter.

If you want to send a custom object, send it in String format using jsonEncode and jsonDecode.

JSON and serialization >> https://docs.flutter.dev/data-and-backend/serialization/json