I try to parse my firebase collection and return a list of Service type. Here I have replaced my firebase call and parsing with a hard code for simplify it.
Future<List<Service>> _getServices() async {
List<Service> _servicesList;
Service service = new Service(
id: 1,
name: "name",
location: "location",
description: "description",
image: "image");
print(service.description);
_servicesList.add(service);
return _servicesList;
}
The print in the function returns the right thing but I got this error :
NoSuchMethodError: The method 'add' was called on null.
That's because you have not initialized _servicesList
List<Service> _servicesList = List<Service>();
EDIT: List
is now deprecated
Try this to create an empty list.
List<Service> _servicesList = [];
OR
To create a fixed length list of size 0.
List<Service> _serviceList = List<Service>.empty();
To create a growable list
List<Service> _serviceList = List<Service>.empty(growable: true);
Refer docs for more details on constructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With