I'm trying to Sort the Date i get from Cloud_Firestore and adding the WeekDay so that the upcoming Date is at the Top and then the Dates more in the Future get behind. Trying this with DateTime.parse i get following Error seen below. I Store only the Date without the WeekDay in Cloud_Firestore.
Can I get the WeekDay just from Parsing or do i need to Store it with the Date?
Error
Invalid date format
21-05-2020
The relevant error-causing widget was
StreamBuilder<QuerySnapshot>
lib/…/MealPlan/mealTile.dart:90
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
Invalid date format
21-05-2020
The relevant error-causing widget was
StreamBuilder<QuerySnapshot>
lib/…/MealPlan/mealTile.dart:90
════════════════════════════════════════════════════════════════════════════════
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) {
DateTime now = doc.data['Date'].toDate();
DateFormat formatter = DateFormat('dd-MM-yyyy');
String formatted = formatter.format(now);
DateTime parsedDate = DateTime.parse(formatted);
String date = formatted;
return Container(
margin: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Meal:',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Text(
'${doc.data['Meal']}',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
SizedBox(height: 20),
Text(
'When:',
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Text(
date,
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
SizedBox(width: 8),
FlatButton(
onPressed: () => deleteData(doc),
child: Row(children: <Widget>[
Text('Delete',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white)),
Icon(Icons.delete_forever, 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);
}
}
MealTile.dart
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) {
DateTime now = doc.data['Date'].toDate();
DateFormat formatter = DateFormat('dd-MM-yyyy');
String formatted = formatter.format(now);
return Container(
margin: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Meal:',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Text(
'${doc.data['Meal']}',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
SizedBox(height: 20),
Text(
'When:',
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Text(
formatted,
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
textAlign: TextAlign.center,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
SizedBox(width: 8),
FlatButton(
onPressed: () => deleteData(doc),
child: Row(children: <Widget>[
Text('Delete',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white)),
Icon(Icons.delete_forever, 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);
}
}