在這個當前代碼段中,我已宣告通知可以為空,但如果回圈的 notification.body 和 notification.title 出現 null 安全錯誤。
這是我的代碼:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
debugPrint('A new onMessageOpenedApp event was published!');
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)],
),
),
);
});
}
});
如果我將“通知”設定為不可為空,我會得到相同的空安全錯誤。
代碼:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
debugPrint('A new onMessageOpenedApp event was published!');
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)],
),
),
);
});
}
});
如何讓它作業?
任何幫助表示贊賞!
編輯:將方法名稱添加到編碼部分。
uj5u.com熱心網友回復:
我假設它的正文和標題可能為空。一個解決方案是回退到空字串,以防它們出現,如下所示:
return AlertDialog(
title: Text(notification.title ?? ''),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body ?? '')],
),
),
);
uj5u.com熱心網友回復:
這里發生了幾件事。首先,您收到 null 錯誤的原因:
RemoteNotification? notification = message.notification;
當notification是一個可為空的值時,當您嘗試使用它而不首先檢查它是否為空時,您將收到警告。將其更改為不可為空可以解決問題,但可能以您實際上并不想要的方式。
現在,當您將其更改為不可為空時,您沒有更改其后的行:
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
Nownotification不再可以為空,但在創建android. 這意味著,即使notification不可為空,android仍然可以為空。由此,您將收到兩個警告 - 一個用于嘗試合并一個不可為空的值,另一個用于使用android不檢查 null 的情況下使用。
最簡單的解決方案是使其也android不可為空:
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification.android;
但這可能是不可能的,這取決于message.notification它本身是否可以為空。
現在說第二件事。假設我們想要notification并且android可以為空,您的第一個空檢查不足以消除回呼中的警告showDialog。這是因為,即使知道何時showDialog呼叫,notification不為空,分析器也不知道回呼本身何時會被呼叫,到那時,notification可能已經變為空。出于這個原因,無論您當前的空檢查如何,分析器都別無選擇,只能顯示警告。
有四種方法可以解決這個問題。首先,您可以再次檢查 null:
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
if (notification != null && android != null) {
return AlertDialog(
title: Text(notification.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)],
),
),
);
} else {
// Return a different widget if null check fails
}
},
);
}
或使用運算子強制非空!:
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification!.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification!.body)],
),
),
);
},
);
}
或者在物件為空的情況下提供默認值:
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification?.title ?? 'Default Value'),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification?.body ?? 'Default Value')],
),
),
);
},
);
}
或者制作回呼可以關閉的物件的非空版本:
if (notification != null && android != null) {
final notificationNN = notification;
final androidNN = android;
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notificationNN.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(androidNN.body)],
),
),
);
},
);
}
uj5u.com熱心網友回復:
試試這個:
...
title: Text(notification.title as String),
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/468636.html
