【发布时间】:2020-07-20 14:09:05
【问题描述】:
我想从 Firestore 数据中显示卡片的颜色。但我收到以下错误。 数据“颜色”以字符串形式存储在 Firestore 中,然后转换为 int。
让我知道如何处理此错误。
这是显示错误的代码:
@override
void initState() {
super.initState();
checkIfColorOrNot();
}
bool selectedColor = false;
checkIfColorOrNot() async {
DocumentSnapshot ds = await Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(widget.user.uid)
.get();
this.setState(() {
selectedColor = ds.exists;
});
}
_colorSelected() {
return StreamBuilder(
stream: selectedColor
? Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(widget.user.uid)
.snapshots()
: Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.snapshots(),
builder: (context, snapshot) {
//checking the snapshot.data is not null before you call snapshot.data.documents.
if (!snapshot.hasData) return CircularProgressIndicator();
var userDocument = snapshot.data;
return userDocument['color'];
},
);
}
@override
Widget build(BuildContext context) {
final length = MediaQuery.of(context).size;
return InkWell(
onTap: widget.onTap,
child: Card(
color: Color(int.parse(_colorSelected())),
elevation: 5,))}
【问题讨论】:
-
streambuilder 是一个小部件,你不需要它!如果您仔细阅读它没有任何意义,因为您同时尝试将循环进度指示器解析为整数(?)。您使用 checkIfColorOrNot 的方式是正确的,您应该使用类似的方法来获取值!
-
抱歉,找不到任何解决方案。需要帮助。
标签: firebase flutter dart google-cloud-firestore