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

2,950

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!

Share:
2,950
Author by

Jyo

Updated on December 15, 2022

Comments

  • Jyo 8 days

    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.

    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,))}
    
    • Cristian Bregant
      Cristian Bregant over 2 years
      The streambuilder is a widget, you don't need that! if you read it carefully it does not make any sense because you are trying to parse to integer a circular progress indicator in the meantime (?). The way you are using with the checkIfColorOrNot is correct and you should something similiar to obtain the value!
    • Jyo over 2 years
      Sorry, could not find any solution for this. Need help.
  • Jyo over 2 years
    I am still not able to get answer for this, can someone help me out?