如何修复MissingPluginException(在通道plugins.flutter.io/path_provider上未找到方法getApplicationDocumentsDirectory的实现)

时间:2020-06-27 12:44:59

标签: flutter asynchronous dart-isolates

您好,我需要在Foreground Service数据包中使用path_provider 前景服务具有隔离功能,因此我需要在隔离功能内使用path_provider,但是当我使用它时,会出现异常

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

任何人都可以帮助我

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");
  }

2 个答案:

答案 0 :(得分:0)

再次运行 flutter cleanflutter run。这应该可以解决问题。

答案 1 :(得分:0)

我也遇到了与您现在面临的 path_provider 包相同的问题,我执行了以下步骤来修复它:

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

问题已解决。