'flutter foreground service Unhandled Exception: MissingPluginException(No implementation found for method getConnectionState on FlutterPlugin)
I have created a flutter plugin with java for use with the watch. and have brought flutter_foreground_task It helps to work after user exits the app so that the app can still sync the clock data at any specified interval for example every 10 minutes, but I have a problem when in the background the app can't run the flutter plugin. I created and got the following error: And when the app runs in the background for a while, the app stops working. Can anyone please give me some advice on how to use background service or foreground service, or is there any other way to suggest? Disturbing all of you help me fix this issue too.
E/flutter (29686): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method getConnectionState on channel releep_watch_connect)
E/flutter (29686): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165)
E/flutter (29686): <asynchronous suspension>
E/flutter (29686): #1 ReleepWatchConnect.getConnectionState (package:releep_watch_connect/releep_watch_connect.dart:28)
E/flutter (29686): <asynchronous suspension>
E/flutter (29686): #2 MyTaskHandler._checkConnectionReleepWatch (package:flutter_app/handler/task_handler.dart:124)
E/flutter (29686): <asynchronous suspension>
Future<bool> _startForegroundTask() async {
// "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted for
// onNotificationPressed function to be called.
//
// When the notification is pressed while permission is denied,
// the onNotificationPressed function is not called and the app opens.
//
// If you do not use the onNotificationPressed or launchApp function,
// you do not need to write this code.
if (!await FlutterForegroundTask.canDrawOverlays) {
final isGranted =
await FlutterForegroundTask.openSystemAlertWindowSettings();
if (!isGranted) {
print('SYSTEM_ALERT_WINDOW permission denied!');
return false;
}
final isGranted2 =
await FlutterForegroundTask.openIgnoreBatteryOptimizationSettings();
if (!isGranted2) {
print('IgnoringBatteryOptimizations permission denied!');
return false;
}
}
// You can save data using the saveData function.
await FlutterForegroundTask.saveData(key: 'customData', value: 'hello');
ReceivePort? receivePort;
if (await FlutterForegroundTask.isRunningService) {
receivePort = await FlutterForegroundTask.restartService();
} else {
receivePort = await FlutterForegroundTask.startService(
notificationTitle: 'Releep Service is running',
notificationText: 'Tap to return to the app',
callback: startCallback,
);
}
return _registerReceivePort(receivePort);
}
// The callback function should always be a top-level function.
void startCallback() {
// The setTaskHandler function must be called to handle the task in the background.
FlutterForegroundTask.setTaskHandler(MyTaskHandler());
}
import 'dart:convert';
import 'dart:isolate';
import 'package:flutter_app/constant.dart';
import 'package:flutter_app/services/api_service2.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:releep_watch_connect/releep_watch_connect.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyTaskHandler extends TaskHandler {
SendPort? _sendPort;
int _eventCount = 0;
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
_sendPort = sendPort;
// You can use the getData function to get the stored data.
final customData =
await FlutterForegroundTask.getData<String>(key: 'customData');
await _getBLEWatchToLocal();
print('customData: $customData');
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
FlutterForegroundTask.updateService(
notificationTitle: 'My App is Working',
notificationText: 'eventCount: $_eventCount'
);
// Send data to the main isolate.
sendPort?.send(_eventCount);
_eventCount++
_getBLEWatchToLocal();
}
@override
Future<void> onDestroy(DateTime timestamp, SendPort? sendPort) async {
// You can use the clearAllData function to clear all the stored data.
await FlutterForegroundTask.clearAllData();
}
@override
void onButtonPressed(String id) {
// Called when the notification button on the Android platform is pressed.
print('onButtonPressed >> $id');
}
@override
void onNotificationPressed() {
// Called when the notification itself on the Android platform is pressed.
//
// "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted for
// this function to be called.
// Note that the app will only route to "/resume-route" when it is exited so
// it will usually be necessary to send a message through the send port to
// signal it to restore state when the app is already started.
FlutterForegroundTask.launchApp("/");
_sendPort?.send('onNotificationPressed');
}
var bleReleepWatch;
Future<void> _getBLEWatchToLocal() async {
//_removeBLEWatchToLocal();
final pref = await SharedPreferences.getInstance();
String listWatchJson = pref.getString(KEY_BLE_WATCH) ?? "";
print("_getBLEWatchToLocal: " + listWatchJson);
if (listWatchJson != "") {
bleReleepWatch = jsonDecode(listWatchJson);
print(bleReleepWatch);
_checkConnectionReleepWatch();
} else {
bleReleepWatch = null;
}
}
Future<int> _connectReleepWatch(watchMac) async {
//_cancelWatchScan();
int code = await ReleepWatchConnect.connectWatch(watchMac);
print("connect Res ${code}");
if (code == 0) {
_syncDataReleepWatch();
}
return code;
}
Future<int> _checkConnectionReleepWatch() async {
//_cancelWatchScan();
int code = await ReleepWatchConnect.getConnectionState();
print("connect Res ${code}");
if (code == 3) {
_connectReleepWatch(bleReleepWatch['MacAddress']);
} else if (code == 5 || code == 6 || code == 10) {
_syncDataReleepWatch();
} else {
print("Somthing Went Wrong");
}
return code;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
