【发布时间】:2020-05-15 15:20:33
【问题描述】:
我正在学习 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());
}
但异常不会被 Android Studio 抛出或捕获,在 logCat 中我可以看到异常,
com.example.flutterlogindemo E/flutter: [错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常: 数据库错误(-3,权限被拒绝,) #0 Query.once(包:firebase_database/src/query.dart:84:41) #1 _HomePageState.initState (package:flutter_login_demo/pages/home_page.dart:48:65)
但找不到在代码中捕获它然后处理异常的方法。
【问题讨论】:
标签: firebase flutter exception dart firebase-realtime-database