I want to store the color as 'color' in Firestore and retrieve it to add the color of my card ;
but when I add new data then it doesn't get added. Maybe i am storing color value as string and Color doesnot support string. so how can i solve this probleM?
the code is given below -
this is where I call Firestore and add documents(there is a document named 'color')
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class FirestoreServices {
final _fireStore = Firestore.instance;
void addNewMatch(int rScore, int bScore) async {
if (_fireStore.collection('matches').snapshots() != null) {
if (rScore > bScore)
await _fireStore.collection('matches').add({
'WinnerText': 'Rikesh Wins',
'RS': rScore,
'BS': bScore,
'Score': ('${rScore.toInt()} - ${bScore.toInt()}'),
'id':
_fireStore.collection('matches').document().documentID.toString(),
'date': DateFormat.yMMMd().format(DateTime.now()),
'color' : Colors.red
});
if (bScore > rScore)
await _fireStore.collection('matches').add({
'WinnerText': 'Bibin Wins',
'RS': rScore,
'BS': bScore,
'Score': ('${bScore.toInt()} - ${rScore.toInt()}'),
'id':
_fireStore.collection('matches').document().documentID.toString(),
'date': DateFormat.yMMMd().format(DateTime.now()),
'color' : Colors.green
});
if (bScore == rScore)
await _fireStore.collection('matches').add({
'WinnerText': 'Drew',
'RS': rScore,
'BS': bScore,
'Score': ('${rScore.toInt()} - ${bScore.toInt()}'),
'id':
_fireStore.collection('matches').document().documentID.toString(),
'date': DateFormat.yMMMd().format(DateTime.now()),
'color' : Colors.green
});
}
}
void removeMatch(id) async {
await _fireStore.collection('matches').document(id).delete();
}
}
--------------------------------------------------
This is my Streamer Page -
import 'package:bvb_firebase/shareable/constants.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class HistoryCardStreamer extends StatefulWidget {
final int rikeshS;
final int bibinS;
HistoryCardStreamer({this.rikeshS, this.bibinS});
@override
_HistoryCardStreamerState createState() => _HistoryCardStreamerState();
}
class _HistoryCardStreamerState extends State<HistoryCardStreamer> {
final _firestore = Firestore.instance;
@override
Widget build(BuildContext context) {
return Container(
height: 300,
child: StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('matches').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(
child: CircularProgressIndicator(),
);
return Container(
height: 300,
child: ListView.builder(
itemCount: snapshot.data.documents.reversed.length,
itemBuilder: (context, index) {
DocumentSnapshot matchDetail = snapshot.data.documents[index];
return Card(
elevation: 0,
color: matchDetail['color'],
child: Container(
margin: EdgeInsets.only(top: 5),
child: ListTile(
title: Text(
matchDetail['WinnerText'],
style: kcardtitleTextStyle,
),
leading: Container(
width: 45,
margin: EdgeInsets.only(top: 12, right: 5),
child: FittedBox(
child: Text(matchDetail['Score'],
style: kcardtitleTextStyle),
),
),
subtitle: Text(
'${DateFormat.yMMMd().format(DateTime.now())}',
style: kcardDateStyle),
trailing: GestureDetector(
onDoubleTap: () async {
await _firestore
.collection('matches')
.document(matchDetail.documentID)
.delete();
},
child: IconButton(
icon: Icon(Icons.delete),
onPressed: () {},
),
),
),
),
);
},
),
);
},
),
);
}
}
//