【发布时间】:2021-09-08 05:48:25
【问题描述】:
- 我正在使用
document['eventDate']直接获取数据库中的时间戳存储我无法显示时间戳所以我将其转换为字符串document['eventDate'].toString(),它给了我你可以在下面看到的输出 - 我尝试将其转换为日期时间格式
DateFormat('dd-MM-yyyy'),但显示错误提示Class 'DateTime' has no instance method 'DateFormat'。 有什么方法可以显示日期。 - 数据以以下格式存储
- 代码
Widget getHomePageBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('events').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: CircularProgressIndicator(),
);
default:
return new ListView(
padding: EdgeInsets.only(bottom: 80),
children: snapshot.data.docs.map((DocumentSnapshot document) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
child: Card(
child: ListTile(
leading: new Image.network(
document['eventImageUrl'],
fit: BoxFit.cover,
width: 120.0,
),
title: new Text(
'Date:' + (document['eventDate'].toDate()).DateFormat('kk:mm:a').format(),
style: new TextStyle(
color: Color(0xFF49DEE8),
fontSize: 14.0,
fontWeight: FontWeight.normal),
),
),
),
);
}).toList(),
);
}
},
);
}
【问题讨论】:
-
关闭,因为firebase时间戳上有很多答案,例如stackoverflow.com/questions/50632217/…
标签: firebase flutter google-cloud-firestore