首页 > 解决方案 > 如何修复 MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现)

问题描述

我需要path_provider在一个ForegroundService数据包内使用。对了Foreground ServiceIsolate,所以我需要在path_provider里面使用Isolate。但是,当我使用它时,我有这个例外:

MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现)

我的代码:

void maybeStartFGS() async {
    ///if the app was killed+relaunched, this function will be executed again
    ///but if the foreground service stayed alive,
    ///this does not need to be re-done

    if (!(await ForegroundService.foregroundServiceIsStarted())) {
      await ForegroundService.setServiceIntervalSeconds(5);

      //necessity of editMode is dubious (see function comments)

      await ForegroundService.notification.startEditMode();
      await ForegroundService.notification
          .setTitle("Example Title: ${DateTime.now()}");
      await ForegroundService.notification
          .setText("Example Text: ${DateTime.now()}");

      await ForegroundService.notification.finishEditMode();

      await ForegroundService.startForegroundService(foregroundServiceFunction);
      await ForegroundService.getWakeLock();
    }

    ///this exists solely in the main app/isolate,
    ///so needs to be redone after every app kill+relaunch
    await ForegroundService.setupIsolateCommunication((data) {
      debugPrint("main received: $data");
     // _beacon.Brodcast();
      //_scanningBeacon.initScanBeacon();
    });
  }

  void foregroundServiceFunction() {

     debugPrint("The current time is: ${DateTime.now()}");
Future<List> readCounter() async {

    try {
      final file = await _localFile;
      print('file$file');
    // Read the file.
   List  reada = await  jsonDecode(file.readAsStringSync());
   print('reada$reada');
    return reada;
    } catch (e) {
print(e.toString());
    return null;
    }
    }
    ForegroundService.notification.setText("The time was: ${DateTime.now()}");

    if (!ForegroundService.isIsolateCommunicationSetup) {
      ForegroundService.setupIsolateCommunication((data) {
        debugPrint("bg isolate received: $data");

      });
    }
    ForegroundService.sendToPort("message from bg isolate");
  }

标签: flutterasynchronousdart-isolates

解决方案


我对 path_provider 包也有同样的问题,就像你现在面临的那样,我做了这些步骤来修复它:

  1. 在 pubspec.yaml 中删除/取消注释包“path_provider”
  2. 在终端上运行颤振酒吧
  3. 使缓存无效并重新启动android studio
  4. 再次在 pubspec.yaml 中取消注释或添加包“path_provider”。
  5. 再次在终端上运行颤振酒吧。

问题已解决。


推荐阅读