颤振中的数组

时间:2020-04-13 13:10:30

标签: flutter dart

我对数组有问题-列表混乱,我在下面的函数中获取了帖子,并且想要将图像添加到其他数组中(一个帖子可能会有1张或更多张图片)

  List posts = [];
  List searchPosts = [];
  List imagesPosts = [];

   _loadPosts() async{
   await Network().getDataPosts('post/getallposts?page=1').then((res) {
    var posts1 = jsonDecode(res.body);
    print(posts1);
    posts = posts1['data']['data'];
    setState(() {  
      posts = searchPosts = posts1['data']['data']; 
    });

    for (var i = 0; i < searchPosts.length; i++) {
        imagesPosts.add(searchPosts[i]['photos_name']);
    }
    print(imagesPosts);

    for (var i = 0; i < searchPosts.length; i++) {
      for (var j = 0; j < imagesPosts[i].length; j++) {
        print(imagesPosts[j]);
      }
    }


   },
   onError: (error) {
    print(error);
   });
  }

Api响应

"success": true,
    "data": {
        "current_page": 1,
        "data": [
            {
                "id": 254,
                "title": "Abc",
                "comments": "sdsadasdasd",
                "photos_name": "[\"2019_11_18_11_14_07_0000001574075647_cdv_photo_026.jpg\",\"2019_11_18_11_14_08_0000001574075648_cdv_photo_027.jpg\"]",
                "lat": 37.96604,
                "longt": 23.732739,
                "created_at": "2019-11-22 16:28:42",
                "updated_at": "2019-11-22 16:28:43",
                "userid": "1",
                "rate": 3,


                }
            },

我收到此错误 未处理的异常:RangeError(索引):无效值:不在0..4范围内,包括5:

我从API获取帖子,帖子包含帖子名称,评论,图片名称 在图像的末尾,我有以下

[["2019_11_18_11_14_07_0000001574075647_cdv_photo_026.jpg","2019_11_18_11_14_08_0000001574075648_cdv_photo_027.jpg"], ["2019_11_19_11_22_20_0000001574162540_cdv_photo_005.jpg"], ["2019_11_19_11_15_43_0000001574162143_cdv_photo_004.jpg"], ["2019_11_19_10_46_53_0000001574160413_cdv_photo_003.jpg"], ["2019_11_19_10_45_07_0000001574160307_cdv_photo_002.jpg"]]

我想我的问题在这里

for (var i = 0; i < searchPosts.length; i++) {
      for (var j = 0; j < imagesPosts[i].length; j++) {
        print(imagesPosts[j]);
      }
    }

当我删除它时,它可以正常工作,但是我无法获取图片帖子

我想在这里显示图像

  for (int i = 0; i <  searchPosts.length; i++) {
  ...
  for (var j = 0; j < imagesPosts[i].length; j++)
                         Image.network("http://127.0.0.1:8004/images/"+searchPosts[i]['userid']+"/"+imagesPosts[j]),
SizedBox(height: 16.0,),

我是新手...

0 个答案:

没有答案