收到错误“dotsCount 必须大于零,断言失败:第 31 行 pos 16:'dotsCount >= 1'”

问题描述 投票:0回答:2

The error i get after running my code

下面是代码示例”

`@覆盖 小部件构建(BuildContext上下文){ 返回列( 孩子们: [ //滑块部分 GetBuilder(构建器:(热门产品){ 返回容器( 高度:Dimentions.pageView, 子:PageView.builder( 控制器:页面控制器, itemCount:popularProducts.popularProductList.length, itemBuilder:(上下文,位置){ 返回 _buildPageItem(位置); }), ); }),

         //dots
        GetBuilder<PopularProductController>(builder: (popularProducts) {
     
          return DotsIndicator(
            dotsCount: popularProducts.popularProductList.length,
            position: _currPageValue,
             decorator: DotsDecorator(
              size: const Size.square(9.0),
             activeColor: AppColors.mainColor,
             activeSize: const Size(18.0, 9.0),
          activeShape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5.0)),
        ),
      );
    }),`

控制器文件

`import 'package:food_app/data/repository/popular_product_repos.dart';
 import 'package:food_app/models/product_model.dart';
 import 'package:get/get.dart';

 class PopularProductController extends GetxController {
 final PopularProductRepository popularProductRepository;

 PopularProductController({required this.popularProductRepository});
 List<dynamic> _popularProductList = [];

 List<dynamic> get popularProductList => _popularProductList;

 Future<void> getPopularProductList() async {
 Response response = await popularProductRepository.getPopularProductList();
 if (response.statusCode == 200) {
  print("Got products");
  _popularProductList = [];
  _popularProductList.addAll(Product.fromJson(response.body).products);
  update();
  } else {}
}
}`
flutter flutter-layout flutter-dependencies flutter-web flutter-test
2个回答
0
投票

该值不能为零:

GetBuilder<PopularProductController>(builder: (popularProducts) {
          print(popularProducts.popularProductList.length);
          return DotsIndicator(
            dotsCount: popularProducts.popularProductList.isNotEmpty
                ? popularProducts.popularProductList.length
                : 2,
            position: _currPageValue,
            decorator: DotsDecorator(
              activeColor: AppColors.mainColor,
              size: const Size.square(9.0),
              activeSize: const Size(18.0, 9.0),
              activeShape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0)),
            ),
          );
        }),


0
投票

**使用这个逻辑**

position:_currentIndex>images.length? _currentIndex=0: _currentIndex,
© www.soinside.com 2019 - 2024. All rights reserved.