我尝试解析我的 firebase 集合并返回服务类型列表。 在这里,我用硬代码替换了我的 firebase 调用和解析以简化它。
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;
}
函数中的 print 返回了正确的东西,但我得到了这个错误:
NoSuchMethodError: The method 'add' was called on null.
最佳答案
那是因为你还没有初始化_servicesList
List<Service> _servicesList = List<Service>();
编辑 List
现已弃用
试试这个来创建一个空列表。
List<Service> _servicesList = [];
或
创建大小为 0 的固定长度列表。
List<Service> _serviceList = List<Service>.empty();
创建一个可增长的列表
List<Service> _serviceList = List<Service>.empty(growable: true);
引用docs有关构造函数的更多详细信息。
关于firebase - 在 null 上调用了方法 'add',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55805850/