我在 MyApp 上設定了提供程式流。
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final AuthService _auth = AuthService();
String? _userId = _auth.user?.uid;
return StreamProvider<UserProfileModel?>(
initialData: null,
create: (_) => DatabaseService().userProfileData(_userId),
builder: (context, snapshot) {
final userT = context.watch<UserProfileModel?>();
final userType = userT?.userType;
print('User type is: $userType');
return MaterialApp(
color: Colors.blueAccent,
debugShowCheckedModeBanner: false,
//home: TOCScreen(),
home: _auth.user == null
? StartScreenMsg()
: userType == 'Creditor'
? CreditorHomePage()
: userType == 'Financial Recovery\nWorker'
? FinancialHomePage()
: userType == 'Debtor'
? DebtorHomePage()
: HomeScreen(),
這從資料庫中獲取 userType 并相應地顯示螢屏,它作業正常,但每當我運行該應用程式時,它都會出現此例外。
════════ Exception caught by provider ══════════════════════════════════════════
The following assertion was thrown:
An exception was throw by _MapStream<DocumentSnapshot<Map<String, dynamic>>, UserProfileModel> listened by
StreamProvider<UserProfileModel?>, but no `catchError` was provided.
Exception:
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
════════════════════════════════════════════════════════════════════════════════
我認為這是因為開始時的 userId 為空,因此,它給出了這個例外。它不會導致崩潰或錯誤,但我不希望在除錯控制臺中顯示例外。這是 userProfileData 代碼
Stream<UserProfileModel> userProfileData(String? user) {
return _firestore
.collection('users')
.doc(user)
.collection('userProfile')
.doc(user)
.snapshots()
.map(_userProfileDataFromSnapshot);
}
任何幫助將不勝感激。
uj5u.com熱心網友回復:
如您上面提供的錯誤所示,這意味著 StreamProvider 具有呼叫的屬性catchError,該屬性需要處理發出的錯誤并在發生錯誤時提供回退。
要捕獲null,您可以采用下面示例中的方法。
StreamProvider(
...
catchError: (_, __) => null,
...
)
您還可以參考此檔案以獲取更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369387.html
標籤:火力基地 扑 镖 谷歌云firestore
