断言失败:第 310 行 pos 10:'data != null':在 onResume 中处理 FCM 消息时出错:回调 Flutter
Failed assertion: line 310 pos 10: 'data != null' : error when handling FCM message in onResume: callback Flutter
我有一个方法,在 3 个 firebase 回调中调用,它处理消息并显示两个不同的底部工作表,具体取决于 message['data]
是否具有自定义参数,以显示消息并执行一些操作逻辑..
当应用程序在前台接收消息时,一切都按预期工作,而当我在应用程序在后台接收消息时,点击横幅时出现 Failed assertion: line 310 pos 10: 'data != null': A non null String must be provided to a Text Widget.
错误。
这是为什么?传递给 onLaunch:
和 onResume:
回调的消息不一样?
非常感谢。
消息处理方法:
void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
title: notification['title'],
body: notification['body'])
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
title: notification['title'],
body: notification['body']),
);
}
}
}
firebase 消息配置:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
handleMessage(message);
},
// onBackgroundMessage: myBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
handleMessage(message);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
handleMessage(message);
},
);
``
好的,我发现,当应用程序在后台并且用户点击横幅时 onResume:
回调被调用,但是 message
地图在那种情况下没有 notification
参数..
这是为什么??我觉得那一点意义都没有..但是嘿..我不工作 Google 所以..
所以我所做的是将 title
和 body
也添加到 POST
请求中的 'data':
,并创建了第二个方法,该方法从 'data':
title
和 body
从 onResume:
和 onLaunch:
.
调用
但话又说回来..我还更改了第一种方法,从'data':
读取title
和body
,这样我就可以使用一个始终有效的方法..
所以现在我还在犹豫使用 'notification':
有什么意义?
void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
final dynamic data = message['data'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
// title: notification['title'],
// body: notification['body'])
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
}
}
我有一个方法,在 3 个 firebase 回调中调用,它处理消息并显示两个不同的底部工作表,具体取决于 message['data]
是否具有自定义参数,以显示消息并执行一些操作逻辑..
当应用程序在前台接收消息时,一切都按预期工作,而当我在应用程序在后台接收消息时,点击横幅时出现 Failed assertion: line 310 pos 10: 'data != null': A non null String must be provided to a Text Widget.
错误。
这是为什么?传递给 onLaunch:
和 onResume:
回调的消息不一样?
非常感谢。
消息处理方法:
void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
title: notification['title'],
body: notification['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
title: notification['title'],
body: notification['body'])
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
title: notification['title'],
body: notification['body']),
);
}
}
}
firebase 消息配置:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
handleMessage(message);
},
// onBackgroundMessage: myBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
handleMessage(message);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
handleMessage(message);
},
);
``
好的,我发现,当应用程序在后台并且用户点击横幅时 onResume:
回调被调用,但是 message
地图在那种情况下没有 notification
参数..
这是为什么??我觉得那一点意义都没有..但是嘿..我不工作 Google 所以..
所以我所做的是将 title
和 body
也添加到 POST
请求中的 'data':
,并创建了第二个方法,该方法从 'data':
title
和 body
从 onResume:
和 onLaunch:
.
但话又说回来..我还更改了第一种方法,从'data':
读取title
和body
,这样我就可以使用一个始终有效的方法..
所以现在我还在犹豫使用 'notification':
有什么意义?
void handleMessage(Map<String, dynamic> message) {
if (message['data'].containsKey('isPromotion')) {
print('on message is promotion message');
final dynamic notification = message['notification'];
final dynamic data = message['data'];
Item item = Item(
itemId: '${data['promotionId']}',
brand: '${data['brand']}',
itemName: '${data['productName']}',
category: '${data['productCategory']}',
price: '${data['price']}',
description: '${data['description']}',
vendor: '${data['vendor']}',
code: '${data['barcode']}',
isPromotion: true,
imageUrl: '${data['imageUrl']}',
);
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationDataBottomSheetIos(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationDataBottomSheetAndroid(
onPressed: (){
BlocProvider.of<CartBloc>(context)
.add(AddItem(item));
Navigator.push(context, MaterialPageRoute(
builder: (_) => PaymentScreen(
bgImage: paymentScreenBgImage,
user: widget.user,
fcmToken: _token,
cityDb: cityDb,
regionDb: regionDb,
countryDb: countryDb,
selectedShop: '${data['vendor']}',
isBuyNow: true,
)
));
},
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
}
else {
print('on message is notification message');
// Handle notification message
final dynamic notification = message['notification'];
final dynamic data = message['data'];
if (Platform.isIOS) {
showCupertinoModalPopup(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetIos(
// title: notification['title'],
// body: notification['body'])
title: data['title'],
body: data['body']),
);
}
else {
//Android
showModalBottomSheet(
context: context,
builder: (modal) => PushNotificationMessageBottomSheetAndroid(
// title: notification['title'],
// body: notification['body']),
title: data['title'],
body: data['body']),
);
}
}
}