为什么 flutter 本地通知会出现异常

Why flutter local notifications give exceptions

我正在尝试使用此依赖项在 android 上发送带有 flutter 的通知:

"flutter_local_notifications: ^9.0.1"

但是当我点击“测试”按钮时出现这个错误。

有人知道我该如何解决吗?

////////////////////////////////////////// //////////////////////////

在main.dart中:

 ElevatedButton(
      onPressed: () => NotificationApi.showNotification(
          title: 'Samira', body: 'Heyy theree !!!', payload: 'samira'),
      child: Text("test"),

在notification.dart中:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
  static final _notifications = FlutterLocalNotificationsPlugin();

  static Future _notificationDetails() async {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        importance: Importance.max,
      ),
      //iOS: IOSNotificationDetails(),
    );
  }

  static Future showNotification({
    int id = 0,
    String? title,
    String? body,
    String? payload,
  }) async =>
      _notifications.show(
        id,
        title,
        body,
        await _notificationDetails(),
        payload: payload,
      );
}


我的控制台中的错误:

Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 1 of 1023 libraries in 426ms.
E/MethodChannel#dexterous.com/flutter/local_notifications(15565): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(15565): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:238)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:180)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:793)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1056)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:949)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:872)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.Looper.loop(Looper.java:136)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.app.ActivityThread.main(ActivityThread.java:6077)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/flutter (15565): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:238)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:180)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:793)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1056)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:949)
E/flutter (15565):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (15565):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/flutter (15565):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:872)
E/flutter (15565):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (15565):  at android.os.MessageQueue.next(MessageQueue.java:323)
E/flutter (15565):  at android.os.Looper.loop(Looper.java:136)
E/flutter (15565):  at android.app.ActivityThread.main(ActivityThread.java:6077)
E/flutter (15565):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (15565):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/flutter (15565):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/flutter (15565): )
E/flutter (15565): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (15565): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter (15565): <asynchronous suspension>
E/flutter (15565): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:194:7)
E/flutter (15565): <asynchronous suspension>
E/flutter (15565): 

您应该正确添加通知图标。它解决了我的问题。

  static Future _notificationDetails() async {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        importance: Importance.max,
        icon: "ic_notification", //<-- Add this parameter
        ),
      //iOS: IOSNotificationDetails(),
    );
  }