简体   繁体   English

我该如何解决这个问题? 构建时引发了以下 NoSuchMethodError:在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](0)

[英]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.

相关问题 在构建 Home() 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;”。 接收方:null 尝试呼叫:&gt;(1) - The following NoSuchMethodError was thrown building Home(): The method '>' was called on null. Receiver: null Tried calling: >(1) 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'data'。 接收方:null 尝试调用:数据 - The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null. Receiver: null Tried calling: data 我该如何解决这个错误? 'NoSuchMethodError' 在 null 上调用了方法 '[]'。 接收器:null。 尝试调用:[]("title") - how can i solve this error? 'NoSuchMethodError' The method '[]' was called on null. Receiver: null. Tried Calling: []("title") 运行时抛出 NoSuchMethodError。 在 null 上调用了方法“[]”。 接收器:null。 尝试调用:[](0)。 如何解决这个错误? - NoSuchMethodError was thrown while running. The method '[]' was called on null. Receiver: null. Tried calling: [](0). How to solve this error? NoSuchMethodError: 在 null 上调用了方法“[]”。 接收者:null 尝试调用:[](&quot;favorites&quot;) - NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("favorites") NoSuchMethodError: 在 null 上调用了方法“+”。 接收者:null 尝试调用:+(&quot; &quot;) - NoSuchMethodError: The method '+' was called on null. Receiver: null Tried calling: +(" ") 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;=”。 接收器:null 相关的错误原因是: - The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was: 错误:在 null 上调用了方法“round”。 接收方:null 尝试调用:round()。 构建小部件时抛出错误 - Error: The method 'round' was called on null. Receiver: null Tried calling: round(). Error thrown when building widget NoSuchMethodError:在 null 上调用了方法“[]”。 I/flutter (23049):接收器:null I/flutter (23049):尝试调用:[]("conversationID") - NoSuchMethodError: The method '[]' was called on null. I/flutter (23049): Receiver: null I/flutter (23049): Tried calling: []("conversationID") NoSuchMethodError:在 null 上调用了方法“[]”。 接收器:null。 尝试调用:[](“名称”) - NoSuchMethodError: The method '[]' was called on null. Receiver: null. Tried calling: [](“name”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM