Search code examples
flutterflutter-layout

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


I am using consumer widget to avoid re-rendering before null safety it works fine... But when I upgrade provider package to it gives me an error which I mentioned above it is not accepting ListView.builder() and says that The return type Widget? isn't a Widget, as required by the closure's context


Consumer<GreatPlaces>(
        child: Center(
          child: const Text(
            'Got no places yet, start adding some',
          ),
        ),
        builder: (ctx, greatPlaces, ch) => greatPlaces.items.length <= 0
            ? ch
            : ListView.builder(          ***//Here I got error***
                itemBuilder: (ctx, index) => Center(),
                itemCount: 5,
              ),
      ),

Solution

  • Use null-assert ! operation on ch!

     data.state.length <= 0
              ? child!
              : ListView.builder(
                  itemBuilder: (context, index) => Container(),
                  itemCount: 4,
                );