Home > OS >  Whey do I get error in "snapshot.data.length"
Whey do I get error in "snapshot.data.length"

Time:09-22

I am getting an error with the streambuilder in flutter. I'm not sure why I get this error. Please help me out with this problem.

Widget _galleryGrid() {
return StreamBuilder(
  stream: imageUrlsController.stream,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      if (snapshot.data!.length != 0) {
        return GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          itemCount: snapshot.data!.length,
          itemBuilder: (context, index) {
            return CachedNetworkImage(
              imageUrl: snapshot.data![index],
              fit: BoxFit.cover,
              placeholder: (context, url) => Container(
                  alignment: Alignment.center,
                  child: CircularProgressIndicator()
              )
            );
          }
        );
      } else {
        return Center(child: Text('No images to display.'));
      }
    } else {
      return Center(child: CircularProgressIndicator());
    }
  }
);

}

I have red underline indicating error in 3 parts in total

if (snapshot.data!.length != 0) {

itemCount: snapshot.data!.length,

imageUrl: snapshot.data![index]

CodePudding user response:

  1. This requires the 'non-nullable' language feature to be enabled.
  2. Try update your pubspec.yaml to set the minimum SDK constraint to 2.12.0 or higher.
  3. Running 'pub get'

OR

replace this with

if (snapshot.data!.isNotEmpty) {

CodePudding user response:

Simply specify your snapshot type in builder of stream as:-

builder: (context, AsyncSnapshot<QuerySnaphot> snapshot) {
  • Related