Reputation: 519
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,
),
),
Upvotes: 14
Views: 22760
Reputation: 63569
Use null-assert !
operation on ch!
data.state.length <= 0
? child!
: ListView.builder(
itemBuilder: (context, index) => Container(),
itemCount: 4,
);
Upvotes: 33
Reputation: 1
Try adding nullcheck to the ch
body:Consumer<GreatPlaces>(
child:Center(
child: Text('Got no places yet, strat adding some!'),
),
builder:(ctx,greatPlaces,ch)=>greatPlaces.items.length<=0 ?
ch!:ListView.builder(
itemCount: greatPlaces.items.length,
itemBuilder: (ctx,i)=>ListTile(),
),
));
Upvotes: 0
Reputation: 4556
Try the following code:
Consumer<GreatPlaces>(
child: Center(
child: const Text(
'Got no places yet, start adding some',
),
),
builder: (ctx, greatPlaces, ch) => greatPlaces.items.length <= 0
? ch ?? Container() // You change Container to other widget
: ListView.builder(
itemBuilder: (ctx, index) => Center(),
itemCount: 5,
),
),
Upvotes: 0
Reputation: 3317
Use below same sample solution ;
body: Consumer<GreatPlaces>(
child: const Center(
child: Text('Got no places yet, start adding some!'),
),
builder: (ctx, greatPlaces, ch) => greatPlaces.items.isEmpty
? ch!
: ListView.builder(
itemBuilder: (ctx, index) => ListTile(
leading: CircleAvatar(
backgroundImage: FileImage(
greatPlaces.items[index].image,
),
),
title: Text(greatPlaces.items[index].title),
onTap: () {},
),
itemCount: greatPlaces.items.length,
),
),
Upvotes: 1