我有 HomePage 類的以下代碼:
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//inicializamos algunos valores
late final FirebaseMessaging _messaging;
late int totalNotificationCounter;
PushNotification? _notificationInfo;
//register Notification
void registerNotification() async {
await Firebase.initializeApp();
//instance for Firebase Messaging
_messaging = FirebaseMessaging.instance;
//hay tres estados de los permisos para recibir la notificacion
//not determined (null), granted (true), declined (false)
NotificationSettings settings = await _messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true
);
if (settings.authorizationStatus == AuthorizationStatus.authorized){
print("User granted the permission");
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
PushNotification notification = PushNotification(
title: message.notification!.title,
body: message.notification!.body,
dataTitle: message.data['title'],
dataBody: message.data['body']
);
setState(() {
totalNotificationCounter ;
_notificationInfo = notification;
});
});
}
}
@override
void initState() {
// TODO: implement initState
totalNotificationCounter = 0;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("PushNotification"),),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text("Flutter Push Notification",textAlign: TextAlign.center,
style: TextStyle(color: Colors.black,fontSize: 20),),
//add notification badge to count total # of notifications received
NotificationBadge(totalNotification: totalNotificationCounter),
],
),
),
);
}
}
這里是 NotificationBadge 類的代碼:
class NotificationBadge extends StatelessWidget {
final int totalNotification;
const NotificationBadge({Key? key, required this.totalNotification}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: 40,
height: 40,
decoration: const BoxDecoration(
color: Colors.orange,
shape: BoxShape.circle,
),
child: Center(
child: Padding(
padding: EdgeInsets.all(8),
child: Text("$totalNotification",style: TextStyle(color: Colors.white,fontSize: 20),),
),
),
);
}
}
線路有問題
NotificationBadge(totalNotification: totalNotificationCounter),
從第一堂課開始。
這是顯示的錯誤:
A value of type 'Null' can't be assigned to a parameter of type 'int' in a const constructor. (Documentation) Try using a subtype, or removing the keyword 'const'.
Invalid constant value.
The values in a const list literal must be constants. (Documentation) Try removing the keyword 'const' from the list literal
我嘗試從 seconde 類中洗掉 const,但顯示了其他錯誤。
uj5u.com熱心網友回復:
問題的原因totalNotificationCounter未初始化,并且totalNotification需要的引數NotificationBadge()不可為空。您可能需要考慮將默認值設定為 totalNotificationCounter,并且僅在該值大于 0 時才顯示 Widget。
IE
Column(
children: [
Text(),
// Return empty Widget if totalNotificationCounter is 0
totalNotificationCounter > 0 ?
NotificationBadge(totalNotification: totalNotificationCounter) : SizedBox(),
]
)
uj5u.com熱心網友回復:
在你的 initState 添加super.initState();
@override
void initState() {
super.initState();
totalNotificationCounter = 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426607.html
