Home > Mobile >  how to catch exception "null operator check on null value" flutter
how to catch exception "null operator check on null value" flutter

Time:10-05

Instead of showing the error message _CastError (Null check operator used on a null value) on debug mode I wanted to show this flutterToast. Is there anyway that I can catch that? code:

String fileName = '';
  String path = '';
  String loading = '';
  String? fullName;
  String? description;

    Future addWidget(String path,String fileName,String fullName,String description,String now) async {
        try {
          DataBase().addWidget(path, fileName, fullName, description, now);
     //How can I catch the Exception here
        } on Exception catch () {
          Fluttertoast.showToast(
            msg: 'Oops something went wrong',
            gravity: ToastGravity.TOP,
            toastLength: Toast.LENGTH_SHORT,
            backgroundColor: Colors.grey[400],
            textColor: Colors.black,
          );
        }
      } 

CodePudding user response:

You can use if condition to do that:

Future addWidget(String path,String fileName,String fullName,String description,String now) async {

        if(fullName == null || description == null || fullName!.isEmpty || description!.isEmpty){
           DataBase().addWidget(path, fileName, fullName, description, now);
        }else {

           Fluttertoast.showToast(
            msg: 'Oops something went wrong',
            gravity: ToastGravity.TOP,
            toastLength: Toast.LENGTH_SHORT,
            backgroundColor: Colors.grey[400],
            textColor: Colors.black,
          );
        }
        
      } 
  • Related