未处理的异常:NoSuchMethodError:在空调用方法"toRawHandle" [英] Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null

查看:90
本文介绍了未处理的异常:NoSuchMethodError:在空调用方法"toRawHandle"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Flutter应用程序中将 firebase_messaging 库用于Firebase推送通知.

i am using firebase_messaging library in My Flutter Application for Firebase Push Notifications.

当前,我的firebase_messaging版本是 firebase_messaging:^ 5.1.5 ,该版本最近更新为最新版本.

Currently my firebase_messaging version is firebase_messaging: ^5.1.5 which was recently updated an the latest one.

我正在尝试在后台以及终止应用程序时接收通知.

i am trying to receive notification in background as well as when application is terminated.

我已按照firebase_messaging文档中提到的所有步骤进行操作,但不幸的是,我在抖动中遇到了上述错误.

i have followed all the steps as mentioned in the documentation of firebase_messaging but unfortunately i am getting the above error in flutter.

这是我在dart中的通知处理程序类

this is my notification handler class in dart

notification_handler.dart

import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;

class NotificationHandler {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  FirebaseMessaging _fcm = FirebaseMessaging();
  StreamSubscription iosSubscription;
  static final NotificationHandler _singleton =
      new NotificationHandler._internal();

  factory NotificationHandler() {
    return _singleton;
  }
  NotificationHandler._internal();

  Future<dynamic> myBackgroundMessageHandler(
      Map<String, dynamic> message) async {
    print("onLaunch: $message");
    _showBigPictureNotification(message);
    // Or do other work.
  }

  initializeFcmNotification() async {
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

    var initializationSettingsAndroid =
        new AndroidInitializationSettings('ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    if (Platform.isIOS) {
      iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
        // save the token  OR subscribe to a topic here
      });

      _fcm.requestNotificationPermissions(IosNotificationSettings());
    } else {
      _saveDeviceToken();
    }

    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        _showBigPictureNotification(message);
      },
      onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
  }

  /// Get the token, save it to the database for current user
  _saveDeviceToken() async {
    String fcmToken = await _fcm.getToken();
    print("FCM_TOKEN: $fcmToken");
  }

  Future<void> _showBigPictureNotification(message) async {
    var rng = new Random();
    var notifId = rng.nextInt(100);

    var largeIconPath = await _downloadAndSaveImage(
        'https://cdn.pixabay.com/photo/2019/04/21/21/29/pattern-4145023_960_720.jpg',
        'largeIcon');
    var bigPicturePath = await _downloadAndSaveImage(
        'https://cdn.pixabay.com/photo/2019/04/21/21/29/pattern-4145023_960_720.jpg',
        'bigPicture');
    var bigPictureStyleInformation = BigPictureStyleInformation(
        bigPicturePath, BitmapSource.FilePath,
        largeIcon: largeIconPath,
        largeIconBitmapSource: BitmapSource.FilePath,
        contentTitle: message['data']['title'],
        htmlFormatContentTitle: true,
        summaryText: message['data']['body'],
        htmlFormatSummaryText: true);
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        '12', 'trading_id', message['data']['body'],
        importance: Importance.High,
        priority: Priority.High,
        style: AndroidNotificationStyle.BigPicture,
        styleInformation: bigPictureStyleInformation);
    var platformChannelSpecifics =
        NotificationDetails(androidPlatformChannelSpecifics, null);
    await flutterLocalNotificationsPlugin.show(
        notifId,
        message['data']['title'],
        message['data']['body'],
        platformChannelSpecifics,
        payload: message['data']['body']);
  }

  Future<void> _showBigTextNotification(message) async {
    var rng = new Random();
    var notifId = rng.nextInt(100);
    var bigTextStyleInformation = BigTextStyleInformation(
        message['data']['body'],
        htmlFormatBigText: true,
        contentTitle: message['data']['title'],
        htmlFormatContentTitle: true,
        summaryText: message['data']['body'],
        htmlFormatSummaryText: true);
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        '12', 'trading_id', '',
        importance: Importance.High,
        priority: Priority.High,
        style: AndroidNotificationStyle.BigText,
        styleInformation: bigTextStyleInformation);
    var platformChannelSpecifics =
        NotificationDetails(androidPlatformChannelSpecifics, null);
    await flutterLocalNotificationsPlugin.show(
        notifId,
        message['data']['title'],
        message['data']['body'],
        platformChannelSpecifics,
        payload: message['data']['body']);
  }

  Future onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
    // await Navigator.push(
    //   context,
    //   new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
    // );
  }

  Future<void> onDidReceiveLocalNotification(
      int id, String title, String body, String payload) async {
    // display a dialog with the notification details, tap ok to go to another page
  }

  Future<String> _downloadAndSaveImage(String url, String fileName) async {
    var directory = await getApplicationDocumentsDirectory();
    var filePath = '${directory.path}/$fileName';
    var response = await http.get(url);
    var file = File(filePath);
    await file.writeAsBytes(response.bodyBytes);
    return filePath;
  }
}

并且我已经在主屏幕中这样称呼了它

and i have called it like this in my home screen

   @override
  void initState() {
    // TODO: implement initState
    super.initState();
    new NotificationHandler().initializeFcmNotification();
  }

推荐答案

在浏览了几个git线程和stackoverflow线程后,我终于找到了最短的答案,没人能告诉我们:

After surfing for several git threads and stackoverflow threads, I finally found the shortest answer that no one could tell:

只是将手放在全球范围内"

"JUST PUT THE HANDLERS IN GLOBAL SCOPE"

在您的what.dart文件中,只需将_firebaseMessaging和4个处理程序,onMessage,onLaunch等...放置在本地类之外,然后就可以了!没有崩溃!

in your whatever.dart file, just put the _firebaseMessaging and the 4 handlers, onMessage, onLaunch, etc... outside local class, and VOILA!! NO CRASH!

原始:

我使用了 bkmza 答案,但不是针对跨平台的OP修复

I used bkmza answer , but was not OP fix for crossplatform

出于某些原因,设置

void initState() {
super.initState();
    _firebaseMessaging.configure
}

无法正常工作,然后我尝试了

was not working, then I tried

Future.delayed(Duration(seconds: 1), () {
     _firebaseMessaging.configure
    }
);

我可以完美地工作了:)

And I got it working flawlessly :)

也许FCM初始化尚未准备好配置处理程序,在Firebase Core完全加载使其正常工作之后设置了延迟

Maybe FCM initialisations was not ready to configure handlers, setting a delay after Firebase Core fully load made it work properly

这篇关于未处理的异常:NoSuchMethodError:在空调用方法"toRawHandle"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆