Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingPluginException on background (terminated app) #1041

Closed
1 task
godofsleepy opened this issue Apr 24, 2022 · 1 comment
Closed
1 task

MissingPluginException on background (terminated app) #1041

godofsleepy opened this issue Apr 24, 2022 · 1 comment

Comments

@godofsleepy
Copy link

godofsleepy commented Apr 24, 2022

馃悰 Bug Report

First thanks for the package.
I'm working on a Background tracking in which I'm using the geolocator: ^8.2.0 & background_fetch: ^1.1.0 package.
i develop background 2 month ago and theres no problem but when i test again, it reproduce error.
before geolocator i had same issue with shared_preferences: ^2.0.13 and i solve it using this tips here, do you have any suggestion what should i do ?

LOG

04-24 22:25:40.459 10759 10759 D TSBackgroundFetch: - Background Fetch event received: flutter_background_fetch

04-24 22:25:40.479 10759 10759 D TSBackgroundFetch: 馃拃 [HeadlessTask flutter_background_fetch]

04-24 22:25:40.813 10759 10759 D TSBackgroundFetch: [HeadlessTask] waiting for client to initialize

04-24 22:25:40.895 10759 10817 I flutter : Observatory listening on http://127.0.0.1:40865/tpX0LuJcqcs=/

04-24 22:25:41.578 10759 10759 I TSBackgroundFetch: $ initialized

04-24 22:25:41.693 10759 10807 I flutter : MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)

04-24 22:25:41.695 10759 10759 D TSBackgroundFetch: - finish: flutter_background_fetch

04-24 22:25:41.695 10759 10759 D TSBackgroundFetch: - jobFinished

Expected behavior

return position value

Reproduction steps

// [Android-only] This "Headless Task" is run when the Android app
// is terminated with enableHeadless: true
void backgroundFetchHeadlessTask(HeadlessTask task) async {
  String taskId = task.taskId;
  bool isTimeout = task.timeout;
  if (isTimeout) {
    // This task has exceeded its allowed running-time.
    // You must stop what you're doing and immediately .finish(taskId)
    BackgroundFetch.finish(taskId);
    return;
  }

  SharedPreferencesAndroid.registerWith();
  final preferences = await SharedPreferences.getInstance();
  String defaultBasedUrl = 'some secret url';
  String basedUrl = preferences.getString(userIpKey) ?? defaultBasedUrl;
  String url = "$basedUrl/api$updateLocationPath";
  Dio dio = Dio(BaseOptions(headers: {
    "Token": preferences.getString(userTokenKey) ?? ''
  })); // Getting Headers and Other data

  if (url.contains('https')) {
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      return client;
    };
  }

  try {
    final location = await Geolocator.getCurrentPosition();
    var params = {
      'latitude': location.latitude,
      'longitude': location.longitude,
      'job_order': preferences.getString(currentjoborderkey) ?? ''
    };
    final response = await dio.post(url, data: params);
    if (response.data['code'] == 401) {
      BackgroundFetch.stop();
    }
  } catch (e) {
    debugPrint(e.toString());
  }
  BackgroundFetch.finish(taskId);
}

Configuration

[鉁揮 Flutter (Channel stable, 2.10.5, on macOS 11.5.2 20G95 darwin-x64, locale en-ID)
[鉁揮 Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[鉁揮 Xcode - develop for iOS and macOS (Xcode 13.2.1)
[鉁揮 Android Studio (version 2021.1)
[鉁揮 VS Code (version 1.66.2)
[!] Connected device
! No devices available
[鉁揮 HTTP Host Availability

Version: 8.2.0

Platform:

  • 馃 Androi
@mvanbeusekom
Copy link
Member

Hi @godofsleepy,

With version 3.1.6 of the geolocator_android package we have moved the package internal interface implementation from the geolocator_platform_interface into the geolocator_android package (as recommended by the Flutter Federated architecture).

After some extensive debugging and researching I found out that this move has one side-effect. Namely it relies on the Flutter engine to register the correct plugin (basically call the GeolocatorAndroid.registerWith() method). The same problem the SharedPreferences plugin is facing. The solution to this problem is documented here.

TL;DR;

  1. If you are on Flutter 2.11+ make sure to call DartPluginRegistrant.ensureInitialized() at the start of your isolate;
  2. For older versions of Flutter please call the registerWith() method for each plugin you'd like to use in your isolate (in your case GeolocatorAndroid.registerWith();

Since this is an issue that has been addressed by the Flutter team with the instruction to developers to call DartPluginRegistrant.ensureInitialized() at the start of your isolate I am going to close this issue. If you run into further issues please feel free to leave a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants