仍在學習顫振,但我認為這是一個非常簡單的問題。我有兩個字串,一個是在小部件狀態中定義的串列。
List<String> uploadedFileUrls1;
String uploadedFileUrl2;
然后用上傳的媒體(影像或視頻)填充每個字串。然后這些用于更新 Firebase 中的記錄,arrayUnion如下所示。
final postsUpdateData = {
...createPostsRecordData()
//this is where I need to check if empty or null
'postAssets': FieldValue.arrayUnion(uploadedFileUrls1 [uploadedFileUrl2]),
};
await columnPostsRecord.reference.update(postsUpdateData);
我的問題是,您如何檢查是否string為空或相互獨立,而不是為該特定字串寫入資料庫。它目前正在寫一個空字串,如果字串為空,我不需要它做任何事情,甚至不寫一個空值。但是,如果填充了一個字串,我需要它來將該字串添加到陣列中,但對于空字串則不需要。設定新變數是唯一的方法嗎?謝謝你的幫助!
uj5u.com熱心網友回復:
您可以只使用String.lengthandList.length方法來檢查任何一個字串是否為空!所以你的代碼看起來像這樣:
List<String> uploadedFileUrls1 = ['a', 'b', 'c'];
String uploadedFileUrl2 = '';
if (uploadedFileUrls1.length != 0 && uploadedFileUrl2.length != 0) {
print('both has data');
// process your update
} else {
print('no data');
// notify user or do something
}
希望這會有所幫助:D
uj5u.com熱心網友回復:
'postAssets': (FieldValue.arrayUnion(uploadedFileUrls1.removeWhere((item) => ["", null].contains(item)).toList() [uploadedFileUrl2])),
嘗試這個
uj5u.com熱心網友回復:
這可能不是最好的方法,但這是我可以弄清楚如何獨立檢查每個,然后相應地更新陣列的唯一方法。此代碼按預期作業。
if (uploadedFileUrls1 !=null && uploadedFileUrl2 !=null)
'postAssets':FieldValue.arrayUnion(uploadedFileUrls1.whereNotNull().toList() [uploadedFileUrl2].whereNotNull().toList()),
if (uploadedFileUrls1 !=null && uploadedFileUrl2 == null)
'postAssets':FieldValue.arrayUnion(uploadedFileUrls1.whereNotNull().toList()),
if (uploadedFileUrls1 ==null && uploadedFileUrl2 != null)
'postAssets':FieldValue.arrayUnion([uploadedFileUrl2].whereNotNull().toList()),
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/490341.html
