[Path Provider] MissingPluginException(No implementation found for method getApplicationDocumentsDirectory when app is in the background

Describe the bug
For example when using path_provider inside a BackgroundMessageHandler the handler stops execution with an error like this:
I/flutter ( 4554): FlutterFire Messaging: An error occurred in your background messaging handler: I/flutter ( 4554): MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)`
Steps to reproduce

Steps to reproduce the behavior:

Take the [example project](https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/firebase_messaging/example)
Add [path_provider](https://pub.dev/packages/path_provider) to the pubspec.yaml.
Call getApplicationDocumentsDirectory in main.dart's _firebaseMessagingBackgroundHandler.
Run the app and put in the background in order to use the BackgroundMessageHandler.
Send a message through firebase.

Expected behavior

I expected that getApplicationDocumentsDirectory would return the same value as if i run it in the main method and not to throw an error.
Sample project

If you use the example project for firebase messageing the only difference is to add path_provider to the pubspec.yaml and edit the main.dart beginning like this.

import 'dart:async';
import 'dart:convert';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_messaging_example/firebase_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:path_provider/path_provider.dart';

import 'message.dart';
import 'message_list.dart';
import 'permissions.dart';
import 'token_monitor.dart';

/// Define a top-level named handler which background/terminated messages will
/// call.
///
/// To verify things are working, check out the native platform logs.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  WidgetsFlutterBinding.ensureInitialized();
  print(getApplicationDocumentsDirectory());  // this line produces a MissingPluginException
  await Firebase.initializeApp(options: DefaultFirebaseConfig.platformOptions);
  print('Handling a background message ${message.messageId}');
}

/// Create a [AndroidNotificationChannel] for heads up notifications
late AndroidNotificationChannel channel;

/// Initialize the [FlutterLocalNotificationsPlugin] package.
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  print(getApplicationDocumentsDirectory());  // however this does not.
  await Firebase.initializeApp(
    options: const FirebaseOptions(
      apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
      appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
      messagingSenderId: '448618578101',
      projectId: 'react-native-firebase-testing',
    ),
  );

  // Set the background messaging handler early on, as a named top-level function
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  // the rest of the file

Additional context

I tried several fixes including a clean build an even uninstalling the sdk completely an re-clone the project to make sure I have no build files leftover on my pc.

A coworker also build the build the project but clean an for the first time.
His machine did not have this problem.

Flutter doctor

Run flutter doctor and paste the output below:

$ /opt/sdk/flutter/bin/flutter doctor -v 
[✓] Flutter (Channel stable, 2.10.2, on Arch Linux 5.16.11-arch1-1, locale en_US.UTF-8)
    • Flutter version 2.10.2 at /opt/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 097d3313d8 (11 days ago), 2022-02-18 19:33:08 -0600
    • Engine revision a83ed0e5e3
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /opt/sdk/android
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 13.0.1
    • cmake version 3.22.2
    • ninja version 1.10.2
    • pkg-config version 1.8.0

[✓] Android Studio (version 2021.1)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 64.1.2
    • Dart plugin version 211.7798
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.3)
    • IntelliJ at /opt/intellij-idea-ultimate-edition
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 12 (API 32) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Arch Linux 5.16.11-arch1-1
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 98.0.4758.102

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
`
```

1 thought on “[Path Provider] MissingPluginException(No implementation found for method getApplicationDocumentsDirectory when app is in the background

Comments are closed.