【问题标题】:Flutter: Using path provider when app is in backgroundFlutter:当应用程序在后台时使用路径提供程序
【发布时间】:2020-10-18 18:26:39
【问题描述】:

我目前正在尝试在我的 Flutter 应用中实现 FCM 和本地通知。我已经成功地为普通通知配置了 FCM 和本地通知,但我也有不同类型的通知,我想用图像显示,当我的应用程序在前台时,通知显示没有错误,但是当我终止时应用程序/将其移至后台尝试使用路径提供程序保存图像时出现异常。

例外:

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)

我假设发生此错误是因为路径提供程序方法通道在应用程序不在前台时关闭,我可以做些什么来解决这个问题?或者如果不是flutter_local_notifications 插件需要位图的文件路径,我可以实现保存图像并以不同的方式获取在后台工作的路径(没有路径提供程序)? (我实际上想要显示的是来自这样一个链接的图像:https://is1-ssl.mzstatic.com/image/thumb/WNUBiv2P6YSklHn9eA5nlg/1000x1000bb.jpeg

保存图片:

 static Future<String> saveImage(Image image) {
    final completer = Completer<String>();
    image.image.resolve(ImageConfiguration()).addListener(ImageStreamListener((imageInfo,_) async {
      final byteData = await imageInfo.image.toByteData(format: ImageByteFormat.png);
      final pngBytes = byteData.buffer.asUint8List();
      final fileName = pngBytes.hashCode;
      final directory = await getApplicationDocumentsDirectory();
      final filePath = '${directory.path}/$fileName';
      final file = File(filePath);
      await file.writeAsBytes(pngBytes);
      completer.complete(filePath);
    }));
    return completer.future;
  }

【问题讨论】:

    标签: image flutter notifications firebase-cloud-messaging flutter-method-channel


    【解决方案1】:

    您还需要在 Application.java 中注册路径提供程序。

    import io.flutter.plugins.pathprovider.PathProviderPlugin;
    
    ...
    
        @Override
        public void registerWith(PluginRegistry registry) {
    PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"));
        }
    

    既然 Flutter 使用 Kotlin 作为 android 端的默认语言,这里是 Kotlin 代码:

    override fun registerWith(registry: PluginRegistry) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"))
    }
    

    【讨论】:

    • 你能帮我明确地为 IOS (Swift) 注册同一个包吗
    【解决方案2】:

    这个错误是因为你没有在android MainAcitivity中注册flutter插件。

    您必须更改 MainActivityonCreate 方法,如下所示:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this.getFlutterEngine());
    }
    

    【讨论】:

    • 我在尝试这个时遇到错误,我认为this.getFlutterEngine 返回null,我在答案中更新了我的代码,无论如何在configureFlutterEngine 中注册不起作用?
    猜你喜欢
    • 2019-05-30
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多