Flutter firebase onBackgroundMessage 抛出异常

时间:2021-01-18 06:52:57

标签: flutter firebase-cloud-messaging

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

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

1 个答案:

答案 0 :(得分:0)

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

来自代码文档

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