【问题标题】:Flutter firebase onBackgroundMessage throws exceptionFlutter firebase onBackgroundMessage 抛出异常
【发布时间】:2021-04-22 11:36:22
【问题描述】:

当我将此函数添加到我的代码中时。

FirebaseMessaging.onBackgroundMessage(
        (message) => _handleBG(message, currentUserId)); 

我得到异常

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null.

下面的 onMessage 方法工作正常。

FirebaseMessaging.onMessage.listen((event) async {
          //  event.messageType
          print('hearing to  foreground messsage');
          if (event.data['messageType'] == 'chat') {
            await ChatHelper.handleMessageReceived(event, context, currentUserId);
            //setState(() {});
    
          }
        });
    
        FirebaseMessaging.onBackgroundMessage(
            (message) => _handleBG(message, currentUserId));

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    后台消息处理程序应声明为顶级函数,以便可以独立调用它,因为您永远不知道收到通知时应用程序及其对象的状态。看起来您的 _handleBG 方法是仅对特定类可用的私有方法。

    来自代码文档

    /// This provided handler must be a top-level function and cannot be
    /// anonymous otherwise an [ArgumentError] will be thrown.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-23
      • 2021-05-26
      • 2021-02-04
      • 2019-07-25
      • 2021-09-02
      • 2020-03-10
      • 2023-01-25
      • 2013-05-24
      相关资源
      最近更新 更多