Home > Enterprise >  Flutter List<dynamic> is not a subtype of type List<String>
Flutter List<dynamic> is not a subtype of type List<String>

Time:10-24

I have a json file with following structure:

[
  {
     "name": "Kejadian",
     "playList": "Kejadian",
     "videos": [
      "q2ZZlUsnX5Y",
      "ANpas6oyZjw",
      "5qwP-SOEdKg",
      "hWm62l-d3DE",
      "F07RJdvQ77w",
    ]
  },
  ...
]

I can get all the data of this list except for videos array, when I try to get string list of my videos it says:

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

here is my code:

my json file class

class VideoDetails {
  final String name, playList;
  final List<String> videos;

  VideoDetails({
    required this.name,
    required this.playList,
    required this.videos,
  });

  factory VideoDetails.fromJson(Map<String, dynamic> json) {
    return VideoDetails(
      name: json['name'],
      playList: json['playList'],
      videos: json["videos"],
    );
  }
}

getting data from my json file

getPlayerList() async {
    String data = await DefaultAssetBundle.of(context).loadString("assets/lists/v-player.json");
    final jsonResult = jsonDecode(data);
    return jsonResult;
}

returning data of my list in FutureBuilder

FutureBuilder(
  future: getPlayerList(),
  builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
    if (snapshot.data == null) {
      return const Center(child: Text("Loading..."), );
    } else {
      return ListView.builder(
        itemCount: snapshot.data.length,
        itemBuilder: (context, index) {
          var item = snapshot.data[index];
          return ListTile(
            leading: const FaIcon(FontAwesomeIcons.video),
            title: Text(item['name']),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text('${item['videos'].length}'),
                const Icon(Icons.arrow_forward_ios_rounded),
              ],
            ),
            onTap: () {
              if(item['videos'].isNotEmpty) {
                Navigator.pushNamed(context, '/player', arguments: VideoArguments(
                    item['name'],
                    item['playList'],
                    item['videos'], // error caused here.
                ));
              } else {
                //...
              }
            },
          );
        }
      );
    }
  }
)

And in my second screen where I get the list of videos (commented in code above) I have the following code:

class VideoListPlayer extends StatefulWidget {
  final List<String> videosList; // getting videos list
  final String playList;
  const VideoListPlayer({Key? key, required this.videosList, required this.playList}) : super(key: key);

  @override
  _VideoListPlayerState createState() => _VideoListPlayerState();
}

Also here is my argument class where videos list supposed to be send to second screen:

class VideoArguments {
  final String name;
  final String playList;
  final List<String> videos;

  VideoArguments(this.name, this.playList, this.videos);
}

Any suggestion?

CodePudding user response:

I believe json types do not have an explicit type, try changing List<String> videos; to List<dynamic> videos;

CodePudding user response:

Solution

What worked for me to fix this issue was:

List.from()

code

Navigator.pushNamed(context, '/player', arguments: VideoArguments(
  item['name'],
  item['playList'],
  List.from(item['videos']),
));
  • Related