flutter 范围错误(索引):无效值:不在0..1的范围内:3

x9ybnkn6  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(145)

我从互联网上拉数据,但我只能在屏幕上显示1条数据,我得到了大小问题,我如何解决这两个错误?

@override
  Widget build(BuildContext context) {
    return FutureBuilder<List<PostModel>?>(
      future: _postService.fetchPostsItems(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          final result = snapshot.data!;
          return ListView.builder(
            shrinkWrap: true,
            itemCount: result.length,
            itemBuilder: ((context, index) {
              return CustomComponents(
                title: result[index].authorName ?? '',
                subTitle: result[index].createdAt ?? '',
                profileImageUrl: result[index].authorProfileImage ?? '',
                postImageUrl: result[index].media ?? '',
                description: result[index].description ?? '',
                likeCount: result[index].likeCount ?? 0,
                dislikeCount: result[index].disLikeCount ?? 0,
                commentCount: result[index].comments?.length ?? 0,
                commentTitle: result[index].comments?[index].authorName ?? '',
                commentDescription: result[index].description ?? '',
                commentImageUrl: result[index].authorProfileImage ?? '',
              );
            }),
          );
        }
        return const Center(child: CircularProgressIndicator());
      },
    );
  }
rt4zxlrg

rt4zxlrg1#

result[index].comments?[index].

这可能会导致问题。您的ListView与result一样长,但这并不意味着result中的注解与结果长度一样长。

相关问题