Home > front end >  StreamBuilder snapshot.data.length
StreamBuilder snapshot.data.length

Time:03-26

buildProductList() {
    return StreamBuilder(
      initialData: productBloc.getAll(),
      stream: productBloc.getStream,
      builder: (context, snapshot){
        return snapshot.data.length>0? buildProductListItems(snapshot) : Center(child: Text("Oops.."),);},
    );
  }

i was try "?" and"!" didn't work example, snapshot.data?.length>0?

snapshot.!data.length>0? enter image description here didn t work

here error mes.

The getter 'length' isn't defined for the type 'Object'. (Documentation) Try importing the library that defines 'length', correcting the name to the name of an existing getter, or defining a getter or field named 'length'.

CodePudding user response:

try snapshot.data!.length

notice the !

and also read this for clarification: Dart Null Safety

CodePudding user response:

check first if data is List or not like this

List myFun(snapshot) {

return (snapshot.data is List)? snapshot.data as List : []

}

now you can use it like this

myFun(snapshot).length>0? buildProductListItems(snapshot) : Center(child: Text("Oops.."),);
  • Related