[英]How do i fix this issue? The following NoSuchMethodError was thrown building: The method '[]' was called on null. Receiver: null Tried calling: [](0)
@override Widget build(BuildContext context) { return Column( children: [ Padding( padding: EdgeInsets.fromLTRB(10, 10, 10, 10), child: TitleWidget( title: 'You select, We Deliver !', onTap: () => Get.toNamed(RouteHelper.getCategoryRoute())), ), Row( children: [ Expanded( child: SizedBox( height: 350, child: GridView.builder( physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: ResponsiveHelper.isDesktop(context) ? 8 : ResponsiveHelper.isTab(context) ? 6 : 4, crossAxisSpacing: 0.0, mainAxisSpacing: 14.0, ), itemCount: 12, shrinkWrap: true, itemBuilder: (BuildContext context, int index) { return Padding( padding: EdgeInsets.symmetric(horizontal: 0, vertical: 0), child: InkWell( onTap: () => Get.toNamed(RouteHelper.getCategoryProductRoute( categoryController.categoryList[index].id, categoryController.categoryList[index].name, )), child: SizedBox( width: 100, child: Column(children: [ Container( height: 84, width: 84, margin: EdgeInsets.only( left: index == 0 ? 0 : Dimensions.PADDING_SIZE_EXTRA_SMALL, right: Dimensions.PADDING_SIZE_EXTRA_SMALL, ), child: ClipRRect( borderRadius: BorderRadius.circular( Dimensions.RADIUS_SMALL), child: CustomImage( image: '${Get.find<SplashController>().configModel.baseUrls.categoryImageUrl}/${categoryController.categoryList[index].image}', height: 50, width: 50, fit: BoxFit.cover, ), ), ), SizedBox( height: Dimensions.PADDING_SIZE_EXTRA_SMALL), Padding( padding: EdgeInsets.only( right: index == 0 ? Dimensions.PADDING_SIZE_EXTRA_SMALL : 0), child: Text( categoryController.categoryList[index].name, style: robotoMedium.copyWith( fontSize: 11, color: Colors.black87), maxLines: 1, overflow: TextOverflow.ellipsis, textAlign: TextAlign.start, ), ), ]), ), ), ); }, ), ), ), ResponsiveHelper.isMobile(context) ? SizedBox() : categoryController.categoryList != null ? Column( children: [ InkWell( onTap: () { showDialog( context: context, builder: (con) => Dialog( child: Container( height: 550, width: 600, child: CategoryPopUp( categoryController: categoryController, )))); }, child: Padding( padding: EdgeInsets.only( right: Dimensions.PADDING_SIZE_SMALL), child: CircleAvatar( radius: 35, backgroundColor: Theme.of(context).primaryColor, child: Text('view_all'.tr, style: TextStyle( fontSize: Dimensions.PADDING_SIZE_DEFAULT, color: Theme.of(context).cardColor)), ), ), ), SizedBox( height: 10, ) ], ) : CategoryAllShimmer(categoryController: categoryController) ], ), ], ); } }
Heare is CategoryController听到的是 CategoryController
class CategoryController extends GetxController implements GetxService {
final CategoryRepo categoryRepo;
CategoryController({@required this.categoryRepo});
List<CategoryModel> _categoryList;
List<CategoryModel> _subCategoryList;
List<Product> _categoryProductList;
List<Product> _searchProductList = [];
List<bool> _interestSelectedList;
bool _isLoading = false;
int _pageSize;
bool _isSearching = false;
int _subCategoryIndex = 0;
List<CategoryModel> get categoryList => _categoryList;
List<CategoryModel> get subCategoryList => _subCategoryList;
List<Product> get categoryProductList => _categoryProductList;
List<Product> get searchProductList => _searchProductList;
List<bool> get interestSelectedList => _interestSelectedList;
bool get isLoading => _isLoading;
int get pageSize => _pageSize;
bool get isSearching => _isSearching;
int get subCategoryIndex => _subCategoryIndex;
Future<void> getCategoryList(bool reload) async {
if(_categoryList == null || reload) {
Response response = await categoryRepo.getCategoryList();
if (response.statusCode == 200) {
_categoryList = [];
_interestSelectedList = [];
response.body.forEach((category) {
_categoryList.add(CategoryModel.fromJson(category));
_interestSelectedList.add(false);
});
} else {
ApiChecker.checkApi(response);
}
update();
}
}
void getSubCategoryList(String categoryID) async {
_subCategoryIndex = 0;
_subCategoryList = null;
_categoryProductList = null;
Response response = await
categoryRepo.getSubCategoryList(categoryID);
if (response.statusCode == 200) {
_subCategoryList= [];
_subCategoryList.add(CategoryModel(id: int.parse(categoryID), name: 'all'.tr));
response.body.forEach((category) => _subCategoryList.add(CategoryModel.fromJson(category)));
getCategoryProductList(categoryID, '1');
} else {
ApiChecker.checkApi(response);
}
}
void setSubCategoryIndex(int index) {
_subCategoryIndex = index;
getCategoryProductList(_subCategoryList[index].id.toString(), '1');
update();
}
void getCategoryProductList(String categoryID, String offset) async {
if(offset == '1') {
_categoryProductList = null;
_isSearching = false;
}
Response response = await categoryRepo.getCategoryProductList(categoryID, offset);
if (response.statusCode == 200) {
if (offset == '1') {
_categoryProductList = [];
}
_categoryProductList.addAll(ProductModel.fromJson(response.body).products);
_pageSize = ProductModel.fromJson(response.body).totalSize;
_isLoading = false;
} else {
ApiChecker.checkApi(response);
}
update();
}
void searchData(String query, String categoryID) async {
_searchProductList = null;
update();
Response response = await categoryRepo.getSearchData(query, categoryID);
if (response.statusCode == 200) {
_searchProductList = [];
_searchProductList.addAll(ProductModel.fromJson(response.body).products);
} else {
ApiChecker.checkApi(response);
}
update();
}
void toggleSearch() {
_isSearching = !_isSearching;
_searchProductList = [];
if(_categoryProductList != null) {
_searchProductList.addAll(_categoryProductList);
}
update();
}
void showBottomLoader() {
_isLoading = true;
update();
}
Future<bool> saveInterest(List<int> interests) async {
_isLoading = true;
update();
Response response = await categoryRepo.saveUserInterests(interests);
bool _isSuccess;
if(response.statusCode == 200) {
_isSuccess = true;
}else {
_isSuccess = false;
ApiChecker.checkApi(response);
}
_isLoading = false;
update();
return _isSuccess;
}
void addInterestSelection(int index) {
_interestSelectedList[index] = !_interestSelectedList[index];
update();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.