gpt4 book ai didi

firebase - 如何在 Flutter 中从 Firestore 存储和检索颜色

转载 作者:行者123 更新时间:2023-12-03 19:43:53 24 4
gpt4 key购买 nike

我想在 Firestore 中将颜色存储为“颜色”
并检索它以添加我的卡片的颜色;

但是当我添加新数据时,它不会被添加。
也许我将颜色值存储为字符串,而颜色不支持字符串。
那么我该如何解决这个问题呢?

代码如下 -

这是我调用 Firestore 并添加文档的地方(有一个名为“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();
}
}



--------------------------------------------------

这是我的主播页面 -
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: () {},
),
),
),
),
);
},
),
);
},
),
);
}
}

//

最佳答案

基于答案 here您可以将颜色保存为数据存储中的字符串,将其转换为正确格式的字符串,如下所示:

String colorString = color.toString();

像这样,您可以在 Firestore 上保存颜色。

然后在检索它时,您应将其从字符串转换为颜色,为此您可以像这样检索它:
color: new Color(matchDetail['colorString']),

例如,要获取按日期排序的数据,您可以使用以下行进行操作,如 here 所述:
stream: _firestore.collection('matches').orderBy('date').snapshots()

关于firebase - 如何在 Flutter 中从 Firestore 存储和检索颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58812802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com