Mohammed Ali
Mohammed Ali

Reputation: 15

the method '[]' cant be unconditionally invoked because the receiver can be 'null'. when i try fetch data from api

try to use data from api with http package but i get null safty problem the method '[]' cant be unconditionally invoked because the receiver can be 'null

return ListTile(
                leading: CircleAvatar(backgroundImage: NetworkImage(snapShot.data[index]['thumbnailUrl']),),
                title: Text(snapShot.data[index]['title']),
                subtitle: Text("${snapShot.data[index]['id']}"),
              );

Upvotes: 1

Views: 51

Answers (1)

You need to tell dart that the variable is not null (only for the first time), this can be done by the use of ! after the variable, for instance if the error tells that snapShot.data maybe null, type snapShot.data! another example could be snapShot.data[index]['id']!

Upvotes: 1

Related Questions