Home > Software engineering >  The return type 'ListView?' isn't a 'Widget', as required by the closure�
The return type 'ListView?' isn't a 'Widget', as required by the closure�

Time:01-23

I'm trying to make an async method that returns a ListView, so to add it to the body of my Scaffold I used future builder, but got this error.

The return type 'ListView?' isn't a 'Widget', as required by the closure's context.dart

This is my FuturuBuilder code:

FutureBuilder<ListView>(
          future: getWidgets(context, this.residents),
          builder: ((context, snapshot) {
            if (snapshot.hasData) {
              return snapshot.data
            } else {
              return new ListView();
            }
          }),
        )

The "getWidgets" is the method that returns a ListView with the Containers I need, the "return new ListView()" is just a placeholder.

CodePudding user response:

add a ! to snapshot.data:

FutureBuilder<ListView>(
          future: getWidgets(context, this.residents),
          builder: ((context, snapshot) {
            if (snapshot.hasData) {
              return snapshot.data!; // add !
            } else {
              return new ListView();
            }
          }),
        )
  • Related