从Firebase扑捉时间时出错?

时间:2019-12-10 09:53:44

标签: firebase flutter dart google-cloud-firestore

大家好,我正在尝试检索存储在Firebase中的时间和日期,但我返回的时间值以毫秒为单位。有人可以帮我解决这个问题吗? enter image description here

这是我的代码

class _UserListState extends State<UserList> {
  final databaseReference = Firestore.instance;

  getList() async {
    databaseReference
        .collection("bookings")
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      return snapshot.documents;
    });
  }

  @override
  Widget build(BuildContext context) {
    return 
      Scaffold(
        appBar: AppBar(title: Text(widget.isDelete? "Delete User": "User List"),backgroundColor: Colors.indigo,),
        body: ListView(
          padding: EdgeInsets.all(12.0),
          children: <Widget>[
            SizedBox(height: 20.0),
            StreamBuilder<QuerySnapshot>(
                stream: databaseReference.collection('bookings').snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Column(
                      children: snapshot.data.documents.map((doc) {
                        print(doc.data);
                        return ListTile(
                          title:  RichText(
                            text: TextSpan(children: [
                              TextSpan(text: "Boat_id : ${doc.data["boat_id"]}\n"),
                              TextSpan(text: "Grand Total : ${doc.data["grand_total"]}\n"),
                              TextSpan(text: "Number of Passengers : ${doc.data["no_of_passengers"]}\n"),
                              TextSpan(text: "Promo code : ${doc.data["promo_code"]}\n"),
                              TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"]}\n"),
                              TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"]}\n"),
], style: TextStyle(color: Colors.black)),

enter image description here

1 个答案:

答案 0 :(得分:2)

更改此:

TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"]}\n"),
TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"]}\n"),

对此:

TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"].toDate()}\n"),
TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"].toDate()}\n"),

toDate()方法应将timeStamp转换为正常日期,您可以在此处找到该方法:

https://github.com/flutter/plugins/blob/6cd8c677a21f1da0e78b20186be3ba0b0de08cef/packages/cloud_firestore/lib/src/timestamp.dart#L69