我有這樣的代碼:
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseFirestore FS = FirebaseFirestore.instance;
CollectionReference TC = FirebaseFirestore.instance.collection("oneSignal");
var docs = TC.doc("xcG9kfCWnUK7QKbK37qd");
var response = await docs.get();
var veriable = response.data();
print(veriable);
我得到這樣的輸出:
{id: 5138523951385235825832}
我只想要這個輸出的值,即5138523951385235825832. 我怎樣才能做到這一點?
uj5u.com熱心網友回復:
你response是 a DocumentSnapshot,所以response.data()回傳 aMap所有欄位。要從中獲取特定欄位,您可以執行以下操作:
var veriable = response.data() as Map;
print(veriable["id"]);
或者,您可以直接從回應中獲取該欄位:
print(response.get("id"));
對于這樣的情況,我建議將檔案放在手邊,在這種情況下為DocumentSnapshot.
uj5u.com熱心網友回復:
response.data()回傳一個地圖。Map 是一個鍵值對。在 Dart 中,可以使用鍵訪問 Map 的值。
void main() {
var details = {'Usrname':'tom','Password':'pass@123'};
details['Uid'] = 'U1oo1';
print(details);
}
它將產生以下輸出 -
{Usrname: tom, Password: pass@123, Uid: U1oo1}
在您的情況下,您可以使用 id 獲取值veriable['id']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446124.html
