You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter添加flutter_facebook_login后FCM后台调用location.getLocation报错

问题原因

这个报错是因为你使用的flutter_facebook_login: ^3.0.0已经停止维护多年,它内部的插件注册逻辑会破坏Flutter后台Isolate的插件注册流程,导致FCM后台消息处理线程中的location包原生方法通道未完成注册,进而触发MissingPluginException。

解决方案

  • 优先替换废弃依赖:移除flutter_facebook_login,改用目前官方维护的Facebook登录对应Flutter包flutter_facebook_auth,按照包说明完成双端配置即可,该包不会干扰后台Isolate的插件注册逻辑,替换后原有location后台调用逻辑可正常运行。
  • 若暂时无法替换旧依赖,可在原生端手动注册后台需要的插件:
    Android端在自定义Application类的onCreate方法中添加如下代码,给FCM后台引擎手动注册location插件:
    import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingService
    import com.lyokone.location.LocationPlugin
    
    class MyApplication : Application() {
        override fun onCreate() {
            super.onCreate()
            FlutterFirebaseMessagingService.setPluginRegistrant { engine ->
                LocationPlugin.registerWith(engine.registrarFor("lyokone/location"))
                // 其他需要在后台调用的原生插件也可在此处统一注册
            }
        }
    }
    
    iOS端遇到相同问题的话,可在AppDelegate中给FCM后台引擎手动注册Location插件即可。
  • 添加防御性判空逻辑,避免位置获取失败后的空指针异常:
    _locationData = await location.getLocation().catchError((onError) {
      printcolor('getLocation error : $onError', color: DColor.red);
      return null;
    });
    if (_locationData != null) {
      printcolor('location : ${_locationData.latitude},${_locationData.longitude}', color: DColor.green);
    }
    
  • 执行flutter clean清空构建缓存后重新编译运行,排除旧缓存导致的插件注册异常。

内容的提问来源于stack exchange,提问作者許子霆

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 18:09:02