Flutter 为 FCM 的 ShowDialog 在 null 上调用了方法“+”

Flutter The method '+' was called on null for ShowDialog for FCM

我在 flutter 中使用 FCM 在我的应用程序上推送通知,在我的设备通知栏上单击通知时一切正常,它将我路由回应用程序并显示一个对话框,其中包含通知的更多详细信息。但是,当我终止该应用程序并发送一条推送通知,该通知将我路由回该应用程序,而不是向我显示包含通知更多详细信息的对话框时,我收到以下错误“NoSuchMethodError The method '+' was called on null”。下面是我的 onMessage、onResume 和 Onlaunch。

 @override
  void initState() {
    // TODO: implement initState
    super.initState();

final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
    firebaseMessaging.configure(

        onMessage: (Map<String, dynamic> message) async {

     item_id= message['data']['item_id'].toString();
          type_id= message['data']['type_id'].toString();
          item_title= message['data']['item_title'].toString();
          item_description= message['data']['item_description'].toString();
          item_thumbnail_name= message['data']['item_thumbnail_name'].toString();
         

if(type_id=='1' ){


           showGeneralDialog(
              barrierLabel: "General Information",
              barrierDismissible: false,
              barrierColor: Colors.black.withOpacity(0.5),
              transitionDuration: Duration(milliseconds: 1200),
              context: context,
              pageBuilder: (context, anim1, anim2) {
                return Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    height: 400,
                    child: SingleChildScrollView(child:Column(children: [

                      SizedBox(height: 10,),

                      Center(child:Container(
                          padding: EdgeInsets.all(10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [

                              Container(
                                  padding: EdgeInsets.all(0),
                                  child:Material(
                                      color: Colors.white,
                                      child:Text("Information!",style:TextStyle(
                                          fontFamily: 'Montserrat', fontSize: 20.0, color: Colors.black,fontWeight: FontWeight.bold)
                                      ))),
                            ],))),
                     
                      Container(
                          padding: EdgeInsets.all(5),
                          child:Material(
                              color: Colors.white,
                              child:Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [


                                  Stack(children: [

                                    Image.asset("assets/640x360.png",fit: BoxFit.cover,
                                      height: 180,),

                                    Image.network(item_thumbnail_name),



                                  ],),

                                  Center(child:Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_title,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.pink,
                                            fontSize: 15,fontWeight: FontWeight.bold),
                                      ))),

                                  Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_description+"...",
                                        maxLines: 4,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.grey,
                                            fontSize: 12,fontWeight: FontWeight.normal),
                                      )),

                                ],))),
                      Container(
                          padding: EdgeInsets.only(left:5,right:5),
                          child:Row(
                            children: <Widget>[
                              Expanded(child:RaisedButton(
                                color: Colors.blue,
                                onPressed: () {    Navigator.pop(context, false);
                                },
                                textColor: Colors.white,
                                padding: const EdgeInsets.all(0.0),
                                child: Container(
                                  decoration: const BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(3)),
                                    color: Colors.blue,
                                  ),
                                  padding: const EdgeInsets.all(10.0),
                                  child:
                                  const Text('Close',),
                                ),
                              )),


                            ],
                          )),


                    ],)),
                    margin: EdgeInsets.only(bottom: 50, left: 12, right: 12),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                );
              },
              transitionBuilder: (context, anim1, anim2, child) {
                return SlideTransition(
                  position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim1),
                  child: child,
                );
              },
            );

          }


}
 onResume: (Map<String, dynamic> message) async {
      

if(type_id=='1' ){


            showGeneralDialog(
              barrierLabel: "General Information",
              barrierDismissible: false,
              barrierColor: Colors.black.withOpacity(0.5),
              transitionDuration: Duration(milliseconds: 1200),
              context: context,
              pageBuilder: (context, anim1, anim2) {
                return Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    height: 400,
                    child: SingleChildScrollView(child:Column(children: [

                      SizedBox(height: 10,),

                      Center(child:Container(
                          padding: EdgeInsets.all(10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [

                              Container(
                                  padding: EdgeInsets.all(0),
                                  child:Material(
                                      color: Colors.white,
                                      child:Text("Information!",style:TextStyle(
                                          fontFamily: 'Montserrat', fontSize: 20.0, color: Colors.black,fontWeight: FontWeight.bold)
                                      ))),
                            ],))),
                     
                      Container(
                          padding: EdgeInsets.all(5),
                          child:Material(
                              color: Colors.white,
                              child:Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [


                                  Stack(children: [

                                    Image.asset("assets/640x360.png",fit: BoxFit.cover,
                                      height: 180,),

                                    Image.network(item_thumbnail_name),



                                  ],),

                                  Center(child:Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_title,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.pink,
                                            fontSize: 15,fontWeight: FontWeight.bold),
                                      ))),

                                  Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_description+"...",
                                        maxLines: 4,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.grey,
                                            fontSize: 12,fontWeight: FontWeight.normal),
                                      )),

                                ],))),
                      Container(
                          padding: EdgeInsets.only(left:5,right:5),
                          child:Row(
                            children: <Widget>[
                              Expanded(child:RaisedButton(
                                color: Colors.blue,
                                onPressed: () {    Navigator.pop(context, false);
                                },
                                textColor: Colors.white,
                                padding: const EdgeInsets.all(0.0),
                                child: Container(
                                  decoration: const BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(3)),
                                    color: Colors.blue,
                                  ),
                                  padding: const EdgeInsets.all(10.0),
                                  child:
                                  const Text('Close',),
                                ),
                              )),


                            ],
                          )),


                    ],)),
                    margin: EdgeInsets.only(bottom: 50, left: 12, right: 12),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                );
              },
              transitionBuilder: (context, anim1, anim2, child) {
                return SlideTransition(
                  position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim1),
                  child: child,
                );
              },
            );

          }


}  onLaunch: (Map<String, dynamic> message) async {
      


if(type_id=='1' ){


            showGeneralDialog(
              barrierLabel: "General Information",
              barrierDismissible: false,
              barrierColor: Colors.black.withOpacity(0.5),
              transitionDuration: Duration(milliseconds: 1200),
              context: context,
              pageBuilder: (context, anim1, anim2) {
                return Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    height: 400,
                    child: SingleChildScrollView(child:Column(children: [

                      SizedBox(height: 10,),

                      Center(child:Container(
                          padding: EdgeInsets.all(10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [

                              Container(
                                  padding: EdgeInsets.all(0),
                                  child:Material(
                                      color: Colors.white,
                                      child:Text("Information!",style:TextStyle(
                                          fontFamily: 'Montserrat', fontSize: 20.0, color: Colors.black,fontWeight: FontWeight.bold)
                                      ))),
                            ],))),
                     
                      Container(
                          padding: EdgeInsets.all(5),
                          child:Material(
                              color: Colors.white,
                              child:Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [


                                  Stack(children: [

                                    Image.asset("assets/640x360.png",fit: BoxFit.cover,
                                      height: 180,),

                                    Image.network(item_thumbnail_name),



                                  ],),

                                  Center(child:Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_title,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.pink,
                                            fontSize: 15,fontWeight: FontWeight.bold),
                                      ))),

                                  Padding(
                                      padding: EdgeInsets.only(top: 5, bottom: 5),
                                      child: Text(item_description+"...",
                                        maxLines: 4,
                                        style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            color: Colors.grey,
                                            fontSize: 12,fontWeight: FontWeight.normal),
                                      )),

                                ],))),
                      
                      Container(
                          padding: EdgeInsets.only(left:5,right:5),
                          child:Row(
                            children: <Widget>[
                              Expanded(child:RaisedButton(
                                color: Colors.blue,
                                onPressed: () {    Navigator.pop(context, false);
                                },
                                textColor: Colors.white,
                                padding: const EdgeInsets.all(0.0),
                                child: Container(
                                  decoration: const BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(3)),
                                    color: Colors.blue,
                                  ),
                                  padding: const EdgeInsets.all(10.0),
                                  child:
                                  const Text('Close',),
                                ),
                              )),


                            ],
                          )),


                    ],)),
                    margin: EdgeInsets.only(bottom: 50, left: 12, right: 12),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                );
              },
              transitionBuilder: (context, anim1, anim2, child) {
                return SlideTransition(
                  position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim1),
                  child: child,
                );
              },
            );

          }

}


}



我的最佳猜测是您正在此处创建 item_description 变量:

item_description= message['data']['item_description'].toString();

但是 message['data']['item_description'] 在地图中实际上没有值,因此 '+' was called on null 错误。

为了修复它是一个空字符串而不是空字符串,将行更新为:

item_description= message['data']['item_description'].toString() ?? '';

因此,在逐行查看我的代码并进行故障排除后,我能够在 onLaunch 中找到 item_description= message['data']['item_description']; ,因此我将其添加如下:

onLaunch: (Map<String, dynamic> message) async {
 
  item_description= message['data']['item_description'];
    
     
}

将此添加到我的 onLaunch 后,我再次收到错误并显示我的 showdialog。