firebase - 抖动错误: type 'StreamBuilder<DocumentSnapshot>' is not a subtype of type 'String'

标签 firebase flutter dart google-cloud-firestore

我想显示 Firestore 数据中卡片的颜色。但我收到以下错误。 数据“颜色”在 Firestore 中存储为字符串,稍后将其转换为 int。

让我知道如何处理此错误。

enter image description here

这是我的代码,显示错误:

@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/

相关文章:

javascript - Firebase getIdToken 不起作用

dart - 使用 bloc 模式缓存数据

flutter - 应用程序在 Debug模式下工作但不在 Release模式下

flutter - 用 flutter 在黑暗模式下切换图像

flutter - 在小部件测试期间检查颜色?

sass - 如何在Dart上设置Angular Material 选项卡面板(及其选项卡)的样式?

javascript - 将对象插入数组中的键内?

ios - Firebase Crashlytics 自定义日志仅记录单行

Flutter DataTable 排序图标未显示

android - 添加appbrain-sdk后dex文件超过64k :14. 30