英文:
I am getting card with dollars from firebase instead of data in flutter
问题
snapshot.data!.docs.forEach((doc) {
Object? data = doc.data();
propertyCards.add(
Card(
child: ListTile(
title: Text(
(doc.data() as Map<String, dynamic>)['property_name'] ??
''),
subtitle: Text((doc.data()
as Map<String, dynamic>)['property_location'] ??
''),
trailing: Text(
'$${(doc.data() as Map<String, dynamic>)['property_price'] ?? ''}'),
),
),
);
});
我在从Firestore检索数据时遇到了美元符号,不知道为什么无法获取我的Firestore中的property_name、location和price。
英文:
snapshot.data!.docs.forEach((doc) {
Object? data = doc.data();
propertyCards.add(
Card(
child: ListTile(
title: Text(
(doc.data() as Map<String, dynamic>)['property_name'] ??
''),
subtitle: Text((doc.data()
as Map<String, dynamic>)['property_location'] ??
''),
trailing: Text(
'$${(doc.data() as Map<String, dynamic>)['property_price'] ?? ''}'),
),
),
);
});
I am getting the dollar signs while retrieving the data from the firestore, i dont know why i am unable to get the property_name, location and price which are in my firestire.
答案1
得分: 0
以下是您要的翻译内容:
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> movie =
document.data()! as Map<String, dynamic>;
return ListTile(
leading: const Icon(Icons.movie),
trailing: Text('$${movie['property_price'] ?? ''}'),
title: Text(movie['title']),
subtitle: Text(movie['genre']),
);
}).toList(),
);
虽然这是在StreamBuilder中实现的,但在常规的获取调用中也是有效的。我已使用'\$${movie['property_price'] ?? ''}'
来使用模板文字来显示美元货币。
我得到的结果如下图所示
参考:在"Real Time updates"部分检查数据访问
英文:
You can use directly map the doc.data() to get the desired result as follows :
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> movie =
document.data()! as Map<String, dynamic>;
return ListTile(
leading: const Icon(Icons.movie),
trailing: Text('$${movie['property_price'] ?? ''}'),
title: Text(movie['title']),
subtitle: Text(movie['genre']),
);
}).toList(),
);
Although this is implemented in StreamBuilder this is also valid in regular get calls, I have used '\$${movie['property_price'] ?? ''}'
to display money in dollars using template literals.
The result I got is as follows
Reference : Check data access in the Real Time updates section
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论