1 exception dart firebase firebase-realtime-database flutter
我正在学习 Flutter,我想捕获 Flutter 应用程序/设备上应该抛出的异常(因为安全规则正确拒绝它)。
下面是代码
try {
FirebaseDatabase.instance.reference().once().then((DataSnapshot snapshot) {
try {
debugPrint(snapshot.toString());
}
on DatabaseError catch (eIn1) {
debugPrint(' onRoot ' + eIn1.toString());
}
});
}on DatabaseError catch (eOut1) {
debugPrint(' on1 ' + eOut1.toString());
}
try {
FirebaseDatabase.instance.reference().child("todo").once().then((DataSnapshot snapshot) {
try {
debugPrint(snapshot.toString());
}
on DatabaseError catch (eIn2) {
debugPrint(' onNode ' + eIn2.toString());
}
});
}on Exception catch (eOut2) {
debugPrint(' on2 ' + eOut2.toString());
}
Run Code Online (Sandbox Code Playgroud)
但是 Android Studio 永远不会抛出或捕获异常,在 logCat 中我可以看到异常,
com.example.flutterlogindemo E/flutter: [错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常: DatabaseError(-3, 权限被拒绝, ) #0 Query.once (package:firebase_database/src/query .dart:84:41) #1 _HomePageState.initState (包:flutter_login_demo/pages/home_page.dart:48:65)
但找不到在代码中捕获它然后对异常采取行动的方法。
您可以使用catchError来捕获错误:
FirebaseDatabase.instance.reference().child("todo").once().then((DataSnapshot snapshot) {
print(snapshot);
})
.catchError((error) {
print("Something went wrong: ${error.message}");
});
Run Code Online (Sandbox Code Playgroud)
https://api.flutter.dev/flutter/package-async_async/DelegatingFuture/catchError.html
| 归档时间: |
|
| 查看次数: |
1784 次 |
| 最近记录: |