Flutter Error : type 'StreamBuilder<DocumentSnapshot>' is not a subtype of type 'String'

Viewed 3215

I want to show the color of the card from Firestore data. But I get the below error. The data 'color' is stored as a string in Firestore and later it's converted to int.

Let me know how can I handle this error.

enter image description here

Here is my code that shows error:

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

You should use something like:

_colorSelected(){

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

Maybe checking also if the color is there or not!

Give a Text() Widget to your userDocument, if you don't give a widget it will never display a String.

Related