I had some issue and cannot solve it when I followed Food App of EDMT Dev. I use Realtime database of Firebase to connect with my Flutter app but it returned Error "type 'Null' is not a subtype of type 'List' in type cast"
body: FutureBuilder(
future: viewModel.displaygetComicList(),
builder: (context, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting)
return Center(child: CircularProgressIndicator(),);
else
{
var lst = snapshot.data as List<Comic>;
return ListView.builder(
itemCount: lst!=null?lst.length:0,
itemBuilder: (context, index) {
return Text(lst[index].category);
}
);
//return Center(child: Text('Load ok'),);
}
}),
Can anyone help me to solve this error? Thank you very much.
CodePudding user response:
Try this if you doubts feel free to ask
body: FutureBuilder(
future: viewModel.displaygetComicList(),
builder: (context, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting)
return Center(child: CircularProgressIndicator(),);
else if(snapshot.data !=null) // check for null
{
var lst = snapshot.data as List<Comic>;
return ListView.builder(
itemCount: lst!=null?lst.length:0,
itemBuilder: (context, index) {
return Text(lst[index].category);
}
);
//return Center(child: Text('Load ok'),);
}
}),
CodePudding user response:
try to change
var lst = snapshot.data as List<Comic>;
to
var lst = snapshot.data as List<Comic>?;
This way you indicate that it's possible to be null. Your way you say it's definitely not null, but because it is it throws the error