flutter - 如何修复 MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现)
问题描述
我需要path_provider
在一个ForegroundService
数据包内使用。对了Foreground Service
有Isolate
,所以我需要在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");
}
解决方案
我对 path_provider 包也有同样的问题,就像你现在面临的那样,我做了这些步骤来修复它:
- 在 pubspec.yaml 中删除/取消注释包“path_provider”
- 在终端上运行颤振酒吧
- 使缓存无效并重新启动android studio
- 再次在 pubspec.yaml 中取消注释或添加包“path_provider”。
- 再次在终端上运行颤振酒吧。
问题已解决。
推荐阅读
- javascript - 如何在元素属性中设置 JavaScript 对象的数组?
- phonegap - 尽管适用于 Android,但 iOS 的 PhoneGap 构建失败
- reactjs - 尽管状态已更新,但我在 React 中使用 axios 发布请求后 DOM 未更新
- html - div 填充屏幕高度的其余部分
- sql-server - 来自 CTE 的多个 COUNT(DISTINCT)
- php - 如果在php中为真,如何跳过条件?
- ajax - 如果部分预订日,则将 partial_book_days 更改为 fully_booked_days
- csv - Spark CSV GZip 到 Parquet?
- java - 递归选择排序(Java Eclipse Neon 2)
- java - 获取图像并在gridview中显示