Skip to content

Commit

Permalink
feat: throw an error if using FlutterBackgroundService class in worke…
Browse files Browse the repository at this point in the history
…r isolate
  • Loading branch information
ekasetiawans authored and prkay committed Oct 26, 2023
1 parent 78dc190 commit 9a5732c
Showing 1 changed file with 14 additions and 1 deletion.
Expand Up @@ -7,9 +7,13 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_background_service_platform_interface/flutter_background_service_platform_interface.dart';

bool _isMainIsolate = true;

@pragma('vm:entry-point')
Future<void> entrypoint(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();
_isMainIsolate = false;

final service = AndroidServiceInstance._();
final int handle = int.parse(args.first);
final callbackHandle = CallbackHandle.fromRawHandle(handle);
Expand All @@ -29,7 +33,16 @@ class FlutterBackgroundServiceAndroid extends FlutterBackgroundServicePlatform {
FlutterBackgroundServiceAndroid._();
static final FlutterBackgroundServiceAndroid _instance =
FlutterBackgroundServiceAndroid._();
factory FlutterBackgroundServiceAndroid() => _instance;

factory FlutterBackgroundServiceAndroid() {
if (!_isMainIsolate) {
throw Exception(
"This class should only be used in the main isolate (UI App)",
);
}

return _instance;
}

Future<void> _handleMethodCall(MethodCall call) async {
debugPrint(call.method);
Expand Down

0 comments on commit 9a5732c

Please sign in to comment.