我不想在除錯模式下顯示錯誤訊息_CastError (Null check operator used on a null value),而是想顯示這個 flutterToast。無論如何,我能抓住它嗎?代碼:
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,
);
}
}
uj5u.com熱心網友回復:
您可以使用if條件來做到這一點:
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,
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512939.html
標籤:扑镖例外颤振依赖
