Prasath S
Prasath S

Reputation: 4424

in flutter List<dynamic> is not a subtype of List<String>

I called the AttachmentWidget(snapshot.data) the method inside the column widget, So dynamically get the data from the API. After receiving this data, According to the ext i stored into the path List, for example, that is an image extension, I will store as an image path In this case if I get 5 images, I want to path 5 path lists into the Photogrid imageUrls: imgpathList, For this, I got an error as

error message

List<dynamic>  is not a subtype of List<String>

So, How to convert List as a List or is there any way to do that? Thank you

Code here

Widget AttachmentWidget(List<Attachment> attachmentmodel)
  {
    int listsize=attachmentmodel.length;

    var imgpathList = new List();
    var videopathList = new List();
    var audiopathList = new List();


    int images=0;
    int videos=0;
    int audio=0;
    int others=0;

    Set <String> ImageExt = {'jpg','png','jpeg','tiff'};
    Set <String> VideoExt = {'mp4','mpeg4','wmp'};
    Set <String> AudioExt={'mpga'};

    for(int i=0;i<=listsize-1;i++)
      {
        String ext=attachmentmodel[i].ext;
        String path=attachmentmodel[i].path;

        if(ImageExt.contains(ext))
          {
            images=images+1;
            imgpathList.add(path);
          }
        else if(VideoExt.contains(ext))
          {
            videos=videos+1;
            videopathList.add(path);
          }
        else if(AudioExt.contains(ext))
          {
            audio=audio+1;
            audiopathList.add(path);
          }
        else
          {
            others=others+1;
          }
      }


    if(listsize==0)
      {
        return Container();
      }
    else if(listsize==1&&images==1)
      {
        return Container(
          height: 200,
          width: MediaQuery.of(context).size.width,
          child: Image.network(
            imgpathList[0]
          )
        );
      }
    else if(listsize==1&&videos==1)
      {
        return Text('video');
      }
    else if(listsize==1&&audio==1)
      {
        return Text('audio');
      }
    //////////////////////////////list size 2
    else if(listsize==2&&images==2)
      {
        return Container(
            height: 200,
            width: MediaQuery.of(context).size.width,
            child: Row(
              children: <Widget>[
                Image.network(
                    imgpathList[0]
                ),
                Image.network(
                    imgpathList[1]
                ),
              ],
            )
        );
      }
    else if(listsize==2&&videos==2)
    {
      return Text('2 videos');
    }
    else if(listsize==2&&audio==2)
    {
      return Text('2 audios');
    }
    else if(listsize==2&&images==1&&videos==1)
    {
      return Container(
        height: 200,
        width: MediaQuery.of(context).size.width,
        child: Row(
          children: <Widget>[

              Image.network(
              imgpathList[0]
                  ),

            Text('video2')

          ]
        )
      );
    }
    else if(listsize==2&&images==1&&audio==1)
      {
        return Container(
            height: 200,
            width: MediaQuery.of(context).size.width,
            child: Row(
                children: <Widget>[

                  Image.network(
                      imgpathList[0]
                  ),

                  Text('audio')

                ]
            )
        );
      }
      else if(listsize==2&&videos==1&&audio==1)
      {
        return Container(
            height: 200,
            width: MediaQuery.of(context).size.width,
            child: Row(
                children: <Widget>[

                 Text('video'),

                  Text('audio')

                ]
            )
        );
      }
      else if(listsize==3&&images==3)
        {
          return Container(
              height: 200,
              width: MediaQuery.of(context).size.width,
              child: Row(
                  children: <Widget>[
                    Container(
                      height: 200,
                      child: Image.network(
                          imgpathList[0]
                      ),
                    ),
                    Container(
                      height: 200,
                      child: Column(
                        children: <Widget>[
                          Container(
                            height: 100,
                            child: Image.network(
                                imgpathList[1]
                            ),
                          ),
                          Container(
                            height: 100,
                            child: Image.network(
                                imgpathList[2]
                            ),
                          ),
                        ],
                      ),
                    ),
                  ]
              )
          );
        }
    else if(listsize==3&&videos==3)
    {
      return Container(
          height: 200,
          width: MediaQuery.of(context).size.width,
          child: Row(
              children: <Widget>[
                Container(
                    height: 200,
                    child: Text('1')
                ),
                Container(
                  height: 200,
                  child: Column(
                    children: <Widget>[
                      Container(
                          height: 100,
                          child: Text('2')
                      ),
                      Container(
                          height: 100,
                          child: Text('3')
                      ),
                    ],
                  ),
                ),
              ]
          )
      );
    }
    else if(listsize==4&&images==4)
      {
        return Container(
            height: 300,
            width: MediaQuery.of(context).size.width,
            child: Row(
                children: <Widget>[
                  Container(
                      height: 300,
                      child: Column(
                        children: <Widget>[
                          Container(
                            height:150,
                            width: 150,
                            child: Image.network(
                                imgpathList[0]
                            ),
                          ),
                          Container(
                            height:150,
                            width: 150,
                            child: Image.network(
                                imgpathList[1]
                            ),
                          ),
                        ],
                      )
                  ),
                  Container(
                      height: 300,
                      child: Column(
                        children: <Widget>[
                          Container(
                            height:150,
                            width: 150,
                            child: Image.network(
                                imgpathList[2]
                            ),
                          ),
                          Container(
                            height:150,
                            width: 150,
                            child: Image.network(
                                imgpathList[3]
                            ),
                          ),
                        ],
                      )
                  ),
                ]
            )
        );
      }

    else if(listsize>=5&&images>=5)
      {
        return PhotoGrid(
          imageUrls: imgpathList,
          onImageClicked: (i) => print('Image $i was clicked!'),
          onExpandClicked: () => print('Expand Image was clicked'),
          maxImages: 4,
        );

      }
    else
      {
       return Text('list size    $listsize \n images     $images \n  videos    $videos  \n audios  $audio');
      }

    }

Upvotes: 1

Views: 1619

Answers (2)

Prasath S
Prasath S

Reputation: 4424

I'm using this method to convert List<dynamic> to List<String> This one is working fine for me

List<String> strlist = imgpathList.map((s) => s as String).toList();
List<String> strlist2 = strlist.cast<String>();

Upvotes: 0

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27177

This is happening because of you are not providing specific type of list.t.

Try following why to declare list.

List<String> imgpathList = new List();
List<String> videopathList = new List();
List<String> audiopathList = new List();

Upvotes: 1

Related Questions