Reputation: 389
I have this error and i don't know what's wrong
════════ Exception caught by widgets library ═══════════════════════════════════ The following NoSuchMethodError was thrown building HomeScreen(dirty, dependencies: [_InheritedProviderScope, MediaQuery], state: _HomeScreenState#1d9fe): The getter 'length' was called on null. Receiver: null Tried calling: length The relevant error-causing widget was HomeScreen lib\…\View\login.dart:387 When the exception was thrown, this was the stack #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) #1 _HomeScreenState.build package:chroma/…/resident/home.dart:61 #2 StatefulElement.build package:flutter/…/widgets/framework.dart:4744 #3 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:4627 #4 StatefulElement.performRebuild
I want to fetch data by using api service and show it in ListBuilder
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: () {
},
);
},
),
],
),
and this is the class of model
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())),
};
}
and this is the function to call it
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;
}
}
please, can anyone help me in my case !!!
Upvotes: 0
Views: 1129
Reputation: 826
Change itemCount: appProvider.homeService.length,
to :
itemCount: appProvider.homeService?.length ?? 0,
Upvotes: 1