![](/img/trans.png)
[英]save and query data from firestore with timeStamp or firebase server time in flutter
[英]Save data from Firestore Flutter
我想從 FireStore 獲取一些數據。
到目前為止,這是我的代碼:
void getProducts() async {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection("users")
.doc(uid)
.collection('products')
.get();
final fields = querySnapshot.docs.map((doc) => doc.data()).toList();
for (var f in fields) {
print(f);
}
}
output如下:
I/flutter (14637): {name: Milk, stock: 10, id: 1049}
I/flutter (14637): {name: Bread, stock: 2, id: 51}
我想保存每個產品的庫存和 id。 我怎樣才能做到這一點?
所以產品已經“保存”在 Firestore 上,但是如果你想將它們保存到本地存儲,試試這個
...
List<Map<String, dynamic>> products = [];
for(var f in fields)
{
products.add(f);
}
//Create a file in applicationDocumentsDirectory (You will need the path_provider package)
final File file = await savedFile;
file.writeAsStringSync(jsonEncode(products));
static Future<File> get savedFile async {
final Directory directory = await getApplicationDocumentsDirectory();
final String path = "${directory.path}/*any_path*/data.json";
final File file = File(path);
if(!file.existsSync()) file.createSync(recursive: true);
return file;
}
path_provider: https://pub.dev/packages/path_provider
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.