Flutter错误:类型“ StreamBuilder <DocumentSnapshot>”不是类型“ String”的子类型

时间:2020-07-20 14:09:05

标签: 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,))}

2 个答案:

答案 0 :(得分:2)

您应该使用类似的东西:

_colorSelected(){

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

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

答案 1 :(得分:0)

为您的 <div class="card"> <div class="bg-white rounded-18 p-16 relative z-1"> <ul class="list-reset text-16 font-bold flex-grow-0 flex-shrink-0 flex flex-col justify-between"> <li> <div class="mb-18 text-center flex-grow-0 flex-shrink-0"> <h4 class="flex justify-center items-center leading-none text-blue-light" style="margin-bottom: 0rem;"><span class="flex-grow-0 flex-shrink-0 text-24 md:text-48 -ml-14 md:-ml-20">$</span> <span class="flex-grow-0 flex-shrink-0 h1_xl2" id="price">319</span></h4> <p class="text-18 font-normal" style="margin-bottom: 1rem">PER MONTH</p> </div> </li> <li class="mb-4 flex-grow-0 flex-shrink-0"> <div class="mb-8"> <label class="check_box"> <input type="radio" id="m" name="radio" onclick="setPrice()" checked="true"> <div class="flex justify-between flex-wrap text-14 sm:text-16 text-blue-light"><span class="flex-grow-0 flex-shrink-0 text-12 sm:text-16">Myself</span> <span class="flex-grow-0 flex-shrink-0 ml-4" id="myselfSmoke">$319/mo.</span></div> <span class="radiomark"></span> </label> </div> </li> <li class="mb-4 flex-grow-0 flex-shrink-0"> <div class="mb-8"> <label class="check_box"> <input type="radio" id="ms" name="radio" onclick="setPrice()"> <div class="flex justify-between flex-wrap text-14 sm:text-16"><span class="flex-grow-0 flex-shrink-0 text-12 sm:text-16">Myself + Spouse</span> <span class="flex-grow-0 flex-shrink-0 ml-4" id="msSmoke">$639/mo.</span></div> <span class="radiomark"></span> </label> </div> </li> <li class="mb-4 flex-grow-0 flex-shrink-0"> <div class="mb-8"> <label class="check_box"> <input type="radio" id="mc" name="radio" onclick="setPrice()"> <div class="flex justify-between flex-wrap text-14 sm:text-16"><span class="flex-grow-0 flex-shrink-0 text-12 sm:text-16">Myself + Children</span> <span class="flex-grow-0 flex-shrink-0 ml-4" id="mcSmoke">$639/mo.</span></div> <span class="radiomark"></span> </label> </div> </li> <li class="mb-4 flex-grow-0 flex-shrink-0"> <div class="mb-8"> <label class="check_box"> <input type="radio" id="mf" name="radio" onclick="setPrice()"> <div class="flex justify-between flex-wrap text-14 sm:text-16"><span class="flex-grow-0 flex-shrink-0 text-12 sm:text-16">Myself + Family</span> <span class="flex-grow-0 flex-shrink-0 ml-4" id="mfSmoke">$899/mo.</span></div> <span class="radiomark"></span> </label> </div> </li> <li class="flex-grow-0 flex-shrink-0 p-8 -mx-8 rounded-6 bg-blue-light"> <label class="check_box"> <input type="checkbox" name="areYouASmoker" id="smoker" onclick="setPrice()"> <div class="text-white"> <p class="text-14 leading-none text-center mr-32 md:mr-0">Yes. A member uses tobacco.</p> <p class="text-12 leading-none font-normal text-center mr-32">($75 surcharge)</p> </div> <span class="checkmark"></span> </label> </li> </ul> </div> 提供一个 Text() 小部件,如果您不提供小部件,它将永远不会显示字符串。