0

I Store the Meal and the Date in 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. 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?

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

Show Date

Community
  • 1
  • 1
Tempelritter
  • 471
  • 9
  • 22

1 Answers1

1

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,
    ),
VadimKo
  • 210
  • 1
  • 5
  • I get the Hours aswell and how do i remove them? I tried it with .format(yyyy-mm-dd) but then i get this error * ════════ Exception caught by widgets library ═══════════════════════════════════ Class 'DateTime' has no instance method 'format'. Receiver: Instance of 'DateTime' Tried calling: format("yyyy-MM-dd") The relevant error-causing widget was StreamBuilder * – Tempelritter May 18 '20 at 12:19
  • you can check an answer here: https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart in your case you can use ${doc.data['Date'].toDate() instead of DateTime.now() – VadimKo May 18 '20 at 12:41
  • This is what i came up with. var now = '${doc.data['Date'].toDate()}'; var formatter = new DateFormat('dd-MM-yyyy'); String formatted = formatter.format(now); I get at the formatter.format(now) a Error called: String now The argument type 'String' can't be assigned to the parameter type 'DateTime'. – Tempelritter May 18 '20 at 13:45