如何在Flutter应用程序中从Firestore导入地图数据?

huangapple go评论59阅读模式
英文:

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);
    }

此外,Firestore数据库看起来是这样的:如何在Flutter应用程序中从Firestore导入地图数据?

英文:

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&lt;void&gt; getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
    .collection(&#39;orders&#39;)
    .where(&#39;Order id&#39;, isEqualTo: id)
    .get();
if (orderSnapshot.docs.isEmpty) {
  return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData[&#39;Status&#39;];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData[&#39;Date &amp; Time&#39;];
startDate = t.toDate();
print(startDate);
}

Also the firestore database looks like this:

如何在Flutter应用程序中从Firestore导入地图数据?

答案1

得分: 2

你需要将从Firestore检索到的数据转换为它们相应的数据结构类型。

例如:&quot;Address&quot; 将是一个类型为 &lt;String,dynamic&gt;Map

&quot;Products&quot; 将是一个 Map&lt;String,dynamic&gt;List,但将其强制转换为 List 类型的 dynamic 也可以正常工作。

Future&lt;void&gt; getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
    .collection(&#39;orders&#39;)
    .where(&#39;Order id&#39;, isEqualTo: id)
    .get();
if (orderSnapshot.docs.isEmpty) {
  return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData[&#39;Status&#39;];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData[&#39;Date &amp; Time&#39;];
startDate = t.toDate();
print(startDate);

// 新代码如下
var addressMap = orderData[&#39;address&#39;] as Map&lt;String,dynamic&gt;;
var buildingNum = addressMap[&#39;Building Number&#39;];
print(&#39;building number: $buildingNum&#39;);

var productList = orderData[&#39;Products&#39;] as List&lt;dynamic&gt;;
for(var productItemMap in productList){
  print(&#39;product name: ${productItemMap[&#39;name&#39;]}&#39;);
}
}
英文:

You need to cast the data retrieved from firestore into their respective data structure types.

For example: &quot;Address&quot; would be a Map of type &lt;String,dynamic&gt;.

&quot;Products&quot; would be a List of Map&lt;String,dynamic&gt;, but casting to a List of dynamic type will work fine.

Future&lt;void&gt; getOrderDetails(String id) async {
final orderSnapshot = await FirebaseFirestore.instance
    .collection(&#39;orders&#39;)
    .where(&#39;Order id&#39;, isEqualTo: id)
    .get();
if (orderSnapshot.docs.isEmpty) {
  return;
}
final orderData = orderSnapshot.docs.first.data();
status = orderData[&#39;Status&#39;];
print(status);
this.id = id;
print(this.id);
Timestamp t;
t = orderData[&#39;Date &amp; Time&#39;];
startDate = t.toDate();
print(startDate);

//new code below
var addressMap = orderData[&#39;address&#39;] as Map&lt;String,dynamic&gt;;
var buildingNum = addressMap[&#39;Building Number&#39;];
print(&#39;building number: $buildingNum&#39;);

var productList = orderData[&#39;Products&#39;] as List&lt;dynamic&gt;;
for(var productItemMap in productList){
  print(&#39;product name: ${productItemMap[&#39;name&#39;]}&#39;);
}
}

huangapple
  • 本文由 发表于 2023年5月22日 15:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定