【问题标题】:Flutter Error : type 'StreamBuilder<DocumentSnapshot>' is not a subtype of type 'String'Flutter 错误:类型“StreamBuilder<DocumentSnapshot>”不是“String”类型的子类型
【发布时间】: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


【解决方案1】:

你应该使用类似的东西:

_colorSelected(){

    var document = await Firestore.instance.collection('rackBookItems').document('user').get();
   return document.data['color'];
}

也许还要检查颜色是否存在!

【讨论】:

  • 我仍然无法得到答案,有人可以帮助我吗?
【解决方案2】:

给你的userDocument一个Text()小部件,如果你不给它一个小部件,它永远不会显示一个字符串。

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 2021-08-23
    • 2021-05-10
    • 2021-02-17
    • 1970-01-01
    • 2022-07-10
    • 2020-03-26
    • 2021-05-10
    • 2021-05-10
    相关资源
    最近更新 更多