Api 颤振:获得者';长度';被调用为空
我有这个错误,我不知道怎么了 ════════ widgets库捕获到异常 ═══════════════════════════════════ 以下是一个错误 在构建主屏幕时被抛出(脏,依赖项: [\u InheritedProviderScope,MediaQuery],状态: _HomeScreenState(1d9fe):在null上调用了getter“length”。Receiver:null尝试调用:导致错误的相关小部件的长度 异常发生时是主屏幕库\…\View\login.dart:387 抛出,这是堆栈 #0 Object.noSuchMethod(省道:核心补片/对象补片。省道:51:5) #1 _homescrenstate.build包:chroma/../resident/home.dart:61 #2 StatefulElement.build包:flatter/../widgets/framework.dart:4744 #3 ComponentElement.performRebuild包:flatter/../widgets/framework.dart:4627 #4 StatefulElement.PerformBuild 我想使用api服务获取数据并在ListBuilder中显示它Api 颤振:获得者';长度';被调用为空,api,flutter,dart,widget,Api,Flutter,Dart,Widget,我有这个错误,我不知道怎么了 ════════ widgets库捕获到异常 ═══════════════════════════════════ 以下是一个错误 在构建主屏幕时被抛出(脏,依赖项: [\u InheritedProviderScope,MediaQuery],状态: _HomeScreenState(1d9fe):在null上调用了getter“length”。Receiver:null尝试调用:导致错误的相关小部件的长度 异常发生时是主屏幕库\…\View\login.dar
ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: [
GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: appProvider.homeService.length,
itemBuilder: (context, index) {
return HomeServiceList(
service: appProvider.homeService[index],
isShowDate: true,
callback: () {
},
);
},
),
],
),
这是一类模型
class Service {
Service({
this.id,
this.name,
this.homePhoto,
this.subServices,
});
int id;
String name;
dynamic homePhoto;
List<Service> subServices;
factory Service.fromJson(Map<String, dynamic> json) => Service(
id: json["id"],
name: json["name"],
homePhoto: json["home_photo"],
subServices: List<Service>.from(
json["sub_services"].map((x) => Service.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"home_photo": homePhoto,
"sub_services": List<dynamic>.from(subServices.map((x) => x.toJson())),
};
}
类服务{
服务({
这个身份证,
这个名字,
这张照片,
这是子服务,
});
int-id;
字符串名;
动态家庭照片;
列出子服务;
工厂服务.fromJson(映射json)=>服务(
id:json[“id”],
名称:json[“名称”],
homePhoto:json[“home_photo”],
子服务:List.from(
json[“sub_services”].map((x)=>Service.fromJson(x)),
);
映射到JSON()=>{
“id”:id,
“姓名”:姓名,
“家庭照片”:家庭照片,
“子服务”:List.from(subServices.map((x)=>x.toJson()),
};
}
这是调用它的函数
class AppProvider with ChangeNotifier {
List<Service> _homeService;
List<Service> get homeService => _homeService;
bool _isLoading = false;
bool get isLoading => _isLoading;
set isLoading(bool value) {
_isLoading = value;
notifyListeners();
}
Future<dynamic> homeListService() async {
_isLoading = true;
notifyListeners();
print('Starting request');
http.Response response =
await http.get(Environment.home, headers: Environment.requestHeader);
print('Completed request');
print('respond countryList data : ${response.body}');
Map<String, dynamic> res = json.decode(response.body);
var results;
if (res['code'] == 200) {
_homeService = [];
res['services'].forEach((v) {
_homeService.add(new Service.fromJson(v));
});
results = true;
} else {
results =
FailedRequest(code: 400, message: res['massage'], status: false);
}
_isLoading = false;
notifyListeners();
return results;
}
}
class AppProvider和ChangeNotifier{
家庭服务清单;
List get homeService=>\u homeService;
bool_isLoading=false;
bool get isLoading=>\u isLoading;
设置isLoading(布尔值){
_isLoading=值;
notifyListeners();
}
Future homeListService()异步{
_isLoading=true;
notifyListeners();
打印(“启动请求”);
响应=
等待http.get(Environment.home,headers:Environment.requestHeader);
打印(“已完成的请求”);
打印('respond countryList data:${response.body}');
Map res=json.decode(response.body);
var结果;
如果(res['code']==200){
_家庭服务=[];
res['services'].forEach((v){
_homeService.add(newservice.fromJson(v));
});
结果=真;
}否则{
结果=
FailedRequest(代码:400,消息:res['按摩],状态:false);
}
_isLoading=false;
notifyListeners();
返回结果;
}
}
拜托,有人能帮我吗 将
itemCount:appProvider.homeService.length更改为:
itemCount: appProvider.homeService?.length ?? 0,
您的homeService
为空。请确保appProvider.homeService
不为空。@Akif您能告诉我如何修复它吗!你能分享一下appProvider
是如何被用来获取数据的吗?@VictorEronmosele我从2分钟开始就分享了它这能回答你的问题吗?我试过了,错误消失了,但是我没有返回数据,你知道原因吗!!!我认为appProvider.homeService
是空的。你应该检查你的代码逻辑,确保它不是空的。后端开发人员将数据放在这个列表中,所以它不是空的,所以我感到紧张,因为我不知道它返回空的原因,这行:print('respond countryList data:${response.body})代码>你能提供它打印出来的内容吗\u homeService
为空表示您的res['code']==200
不正确。请检查一下。