未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]"(无法在控制台上打印消息) [英] Unhandled Exception: NoSuchMethodError: The method '[ ]' was called on null( Not able to print Message on Console)
问题描述
这是我尝试从 firebase 消息传递消息时出现的错误,如果有人能向我解释如何使其正确,我会很高兴我正在使用旧的依赖项,但切换到新的依赖项会导致很多问题.
Here is the error when im trying to send the message from the firebase messaging i would be really glad if anyone could explain me this how to make it correct I was using old dependencies but swtiching to new is causing Alot of problem.
E/flutter (21035): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter (21035): Receiver: null
E/flutter (21035): Tried calling: []("ride_id")
这是我的pushnotification.dart:
文件
import 'dart:io';
import 'package:cab_driver/globalvariables.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import '../main.dart';
class PushNotificationService {
final FirebaseMessaging fcm = FirebaseMessaging.instance;
Future initialize() async {
FirebaseMessaging.instance.getInitialMessage();
FirebaseMessaging.onMessage.listen((RemoteMessage message) async{
FetchRiderInfo(getRideID(message.data));
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async{
FetchRiderInfo(getRideID(message.data));
});
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
}
Future<String>getToken() async{
String token = await fcm.getToken();
print('token: $token');
DatabaseReference tokenRef = FirebaseDatabase.instance.reference().child('drivers/${currentFirebaseUser.uid}/token');
tokenRef.set(token);
fcm.subscribeToTopic('alldrivers');
fcm.subscribeToTopic('allusers');
}
String getRideID(Map<String, dynamic>message){
String rideID = '';
if(Platform.isAndroid) {
rideID = message['data']['ride_id'];
}
else{
rideID = message['data']['ride_id'];
print('ride_id : $rideID');
}
return rideID;
}
void FetchRiderInfo(String rideId){
DatabaseReference rideRef = FirebaseDatabase.instance.reference().child('rideRequest/$rideId');
rideRef.once().then((DataSnapshot snapshot){
if(snapshot.value != null){
double pickupLat = double.parse(snapshot.value['location']['latitude'].toString());
double pickupLng = double.parse(snapshot.value['location']['longitude'].toString());
String pickupAddress = snapshot.value['pickup_address'].toString();
double destinationLat = double.parse(snapshot.value['destination']['latitude'].toString());
double destinationLng = double.parse(snapshot.value['destination']['longitude'].toString());
String destinationAddress = snapshot.value['destination_address'];
String paymentMethod = snapshot.value['payment_method'];
print(pickupAddress);
}
});
}
}
我想打印我从邮递员那里发送的消息,但给出的信息与 Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
任何人都可以提供帮助我真的卡住了,什么都不懂
I wanted to print the message which i'm sending from the postman but giving the same thing as Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
plz anyone can help Im really stuck not understanding anything
推荐答案
打印message['data']
并查看内容,好像没有ride_id字段代码>.也请查看文档.
Print the message['data']
and check it contents, it seems it has no field called ride_id
. Check the documentation as well.
这篇关于未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]"(无法在控制台上打印消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!