Firebase 颤振错误:类型';StreamBuilder<;文档快照>';不是类型为';字符串';
我想显示Firestore数据中卡的颜色。但是我得到了下面的错误。 数据“color”以字符串形式存储在Firestore中,然后转换为int 让我知道如何处理此错误 以下是显示错误的代码:Firebase 颤振错误:类型';StreamBuilder<;文档快照>';不是类型为';字符串';,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我想显示Firestore数据中卡的颜色。但是我得到了下面的错误。 数据“color”以字符串形式存储在Firestore中,然后转换为int 让我知道如何处理此错误 以下是显示错误的代码: @override void initState() { super.initState(); checkIfColorOrNot(); } bool selectedColor = false; checkIfColorOrNot() async { Docu
@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'];
}
也许还要检查颜色是否存在 给你的
userDocument
一个Text()
小部件,如果你不给小部件,它将永远不会显示字符串。streambuilder是一个小部件,你不需要它!如果您仔细阅读它,它就没有任何意义,因为您正在尝试同时解析为整数的循环进度指示器(?)。您使用CheckIfColorNot的方式是正确的,您应该使用类似的方式来获取值!很抱歉,找不到此问题的任何解决方案。需要帮助。我仍然无法得到答案,有人能帮我吗?