我想显示 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,))}
最佳答案
你应该使用类似的东西:
_colorSelected(){
var document = await Firestore.instance.collection('rackBookItems').document('user').get();
return document.data['color'];
}
也许还可以检查颜色是否存在!
关于firebase - 抖动错误: type 'StreamBuilder<DocumentSnapshot>' is not a subtype of type 'String' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62997238/