android - 在 Flutter ListView 中加载更多选项

标签 android flutter dart flutter-layout

我想部分获取所有无限 ListView 数据,这意味着最初它加载 10 个数据项,并且在滚动更多时它应该获取接下来的 10 个项目。
我正在从我的 Laravel api 获取数据,并且在我的 Laravel api 端点中没有 per_page 选项,所以请帮助我集成加载更多 ListView 数据的选项。
这是我的列表数据。

List<Category> _categoryList = List<Category>();
  CategoryService _categoryService = CategoryService();

  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    _getAllCategories();
  }
  _getAllCategories() async {
    var categories = await _categoryService.getCategories();
    var result = json.decode(categories.body);
    result['data'].forEach((data) {
      var model = Category();
      model.id = data["id"];
      model.name = data["categoryName"];
      model.icon = data["categoryIcon"];
      setState(() {
        _categoryList.add(model);
        isLoading = false;
      });
    });
  }
我正在简单的 ListTile 中获取所有数据。
child: ListView.builder(
        itemCount: //_categoryList.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: _categoryList.name
          );
        },
      ),

最佳答案

所以我有一个技巧,我在我的项目中使用它。我们需要的是在列表末尾时加载更多数据。为此,我们可以使用 ListView.builder()只要:


child: ListView.builder(
       itemCount: _categoryList.length + 1,
       itemBuilder: (context, index) {
         if(index == _categoryList.length){
          // loadMore();
          // return Loading();         
          }
         return ListTile(
            title: _categoryList.name
          );
       }),
所以我们正在做的是我们设置了_categoryList.length + 1itemCount .如果 _categoryList.length10那么我们将有 11 ListView 中的项目并且索引范围将是 0 - 10 .所以在builder里面,我们正在检查 index等于 _categoryList.length这是10 .如果 index等于_categoryList.length的长度,那么我们在 List 的末尾我们可以简单地调用一些函数来从 Api 加载更多数据或显示加载小部件。
我想我的问题是对的,通过这种方式,您可以在用户到达列表末尾时简单地延迟加载数据,而无需使用任何第三方库。

关于android - 在 Flutter ListView 中加载更多选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65198306/

相关文章:

javascript - 悬停和激活时 ionic 更改侧面菜单图标

android - 从坐标获取位置

android - Facebook 深度链接在 Android 中无法使用

java - 对齐 BaseLine 但避免 Android 布局中的循环依赖

firebase - Flutter:Firestore在StreamBuilder中获取用户uid

dart - Angular 组件的应用程序布局使用 .scss 引发错误

flutter - 如何在 flutter 中更改 Radio 或 RadioListTile 的填充?

dart - 拦截器不能在Dio中使用?

firebase - 使用特定路径在 Firebase 托管上部署网站

firebase - 我想在get函数中获取用户ID