简体   繁体   English

Flutter Dart 错误格式时间步从 Cloud_Firestore 到日期

[英]Flutter Dart Error Format Timestep from Cloud_Firestore into Date

I Store the Meal and the Date in Cloud_Firestore.我将膳食和日期存储在 Cloud_Firestore 中。 Getting it back, it looks like this shown on the Screen Show.取回它,它看起来像在屏幕显示上显示的那样。

I tried to formate the Timestamp with DateTime but this doesn't work.我试图用 DateTime 形成时间戳,但这不起作用。 I also tried it with the intl.dart Package to format it, How can I parse the Timestamp and Format it into Year,Month and Day?我还尝试使用 intl.dart Package 对其进行格式化,如何解析时间戳并将其格式化为年、月和日?

Code代码

import 'package:flutter/material.dart';
import 'package:mealapp/models/Widgets/whenAndWhatToEat.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:intl/intl.dart';
import 'package:mealapp/models/global.dart';

class MealTile extends StatefulWidget {
  final MealsAndWhen mealsAndWhen;
  MealTile ({ this.mealsAndWhen });
  
  @override
  MealTileState createState() {
    return MealTileState();
  }
}
class MealTileState extends State<MealTile> {
  String id;
  final db = Firestore.instance;
  String meal;


  Widget buildItem(DocumentSnapshot doc) {
    return Container(
        margin: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(
              'Meal: ${doc.data['Meal']}',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
              textAlign: TextAlign.center,
            ),
            Text(
              'Date: ${doc.data['Date']}',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 12),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                SizedBox(width: 8),
                FlatButton(
                  onPressed: () => deleteData(doc),
                  child: Text('Delete',
                  style: TextStyle(fontWeight: FontWeight.bold,
                  color: Colors.white)
                  ),
                ),
              ],
            ),
          ],
        ),
        decoration: BoxDecoration(
          color: lightBlueColor,
          borderRadius: BorderRadius.all(Radius.circular(12)),
        ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: darkGreyColor,
      body: ListView(
        padding: EdgeInsets.only(top: 220),
        children: <Widget>[
          StreamBuilder<QuerySnapshot>(
            stream: db.collection('mealList').snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Column(children: snapshot.data.documents.map((doc) => buildItem(doc)).toList());
              } else {
                return SizedBox();
              }
            },
          )
        ],
      ),
    );
  }

  void deleteData(DocumentSnapshot doc) async {
    await db.collection('mealList').document(doc.documentID).delete();
    setState(() => id = null);
  }
}

显示日期

You can try to convert it to date before printing.您可以尝试在打印前将其转换为日期。

Text(
     'Date: ${doc.data['Date'].toDate()}',
      style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
      textAlign: TextAlign.center,
    ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM