英文:
how to import a map data from firestore in flutter app?
问题
我是你的中文翻译,以下是翻译好的内容:
我对Flutter还很新手,我在将存储在Firestore中的地图数据导入到Flutter应用程序时遇到了一些问题。我成功地导入了一些数据和一些字符串变量。接下来的一步是导入一组地图。我想要一个关于如何导入地址和产品列表的示例。
我有这段代码:
Future<void> getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
.collection('orders')
.where('Order id', isEqualTo: id)
.get();
if (orderSnapshot.docs.isEmpty) {
return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData['Status'];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData['Date & Time'];
startDate = t.toDate();
print(startDate);
}
英文:
I'm new to flutter and I'm facing some problem with importing a map data stored in firestore back to a flutter app. I succesfully imported a data and some String variables. Also next step after that is to import a List of maps. I want an example about how to import the address and the list of products.
I have this code:
Future<void> getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
.collection('orders')
.where('Order id', isEqualTo: id)
.get();
if (orderSnapshot.docs.isEmpty) {
return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData['Status'];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData['Date & Time'];
startDate = t.toDate();
print(startDate);
}
Also the firestore database looks like this:
答案1
得分: 2
你需要将从Firestore检索到的数据转换为它们相应的数据结构类型。
例如:"Address"
将是一个类型为 <String,dynamic>
的 Map
。
"Products"
将是一个 Map<String,dynamic>
的 List
,但将其强制转换为 List
类型的 dynamic
也可以正常工作。
Future<void> getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
.collection('orders')
.where('Order id', isEqualTo: id)
.get();
if (orderSnapshot.docs.isEmpty) {
return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData['Status'];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData['Date & Time'];
startDate = t.toDate();
print(startDate);
// 新代码如下
var addressMap = orderData['address'] as Map<String,dynamic>;
var buildingNum = addressMap['Building Number'];
print('building number: $buildingNum');
var productList = orderData['Products'] as List<dynamic>;
for(var productItemMap in productList){
print('product name: ${productItemMap['name']}');
}
}
英文:
You need to cast the data retrieved from firestore into their respective data structure types.
For example: "Address"
would be a Map
of type <String,dynamic>
.
"Products"
would be a List
of Map<String,dynamic>
, but casting to a List
of dynamic
type will work fine.
Future<void> getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
.collection('orders')
.where('Order id', isEqualTo: id)
.get();
if (orderSnapshot.docs.isEmpty) {
return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData['Status'];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData['Date & Time'];
startDate = t.toDate();
print(startDate);
//new code below
var addressMap = orderData['address'] as Map<String,dynamic>;
var buildingNum = addressMap['Building Number'];
print('building number: $buildingNum');
var productList = orderData['Products'] as List<dynamic>;
for(var productItemMap in productList){
print('product name: ${productItemMap['name']}');
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论