Future<List<CryptoWalletModel>> getUserWalletData(String uuid) async {
String _dbPath = '${DatabaseGlobals.collectionUsers}/$uuid/${DatabaseGlobals.collectionWallets}';
Logger.logIt('Wallet path:' _dbPath);
final cryptoWalletRef = FirebaseFirestore.instance.collection(_dbPath).withConverter<CryptoWalletModel>(
fromFirestore: (snapshot, _) => CryptoWalletModel.fromJson(snapshot.data()!),
toFirestore: (wallet, _) => wallet.toJson(),
);
List<CryptoWalletModel> _list = [];
List<QueryDocumentSnapshot<CryptoWalletModel>> wallets = await cryptoWalletRef
.get()
.then((snapshot) => snapshot.docs);
try { //Problem Code Here
wallets.forEach((element) {
_list.add(element.data());
});
} catch (e) {
Logger.logIt(e.toString());
}
Logger.logIt('BlocWalletRepoListCount: ' wallets.length.toString());
return _list;
}
很難理解為什么 for each 在完成之前被跳過。我知道錢包中有五個專案,但 wallets.forEach 字串似乎沒有運行。
歡迎任何想法。
uj5u.com熱心網友回復:
在您的代碼中,try塊將在get()完成回傳的未來之前執行。此外,混合async/await和.then語法基本上不是一個好主意。最后,您可以使用 一步將結果轉換為串列map。
試試下面的代碼:
try {
final snapshot = await cryptoWalletRef.get();
_list = snapshot.docs.map((e) => e.data()).toList();
return Future.value(_list);
} catch (e) {
// handler error
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389774.html
