[Flutter] Fix List<dynamic> is not a subtype of type List<String> error

[Flutter] Fix List is not a subtype of type List error

2023-09-05 hit count image

Let's see how to fix List is not a subtype of type List error that occurs when receiving data through the http package and converting it to a model class.

Outline

In Flutter, we use the http package for API communication. For how to call API using the http package in Flutter, please refer to the link below.

In Flutter, we usually store the data received through the http package in a model class and use it. For how to store JSON data received through the http package in a model class, please refer to the official documentation below.

In this blog post, I will introduce how to fix the List<dynamic> is not a subtype of type List<String> error that occurs when receiving data through the http package and converting it to a model class.

Converting type error

When receiving data through the http package and converting it to a model class, we use the fromJson method as follows.

class ExampleModel {
  final List<String>? list;

  ExampleModel({this.list});

  factory ExampleModel.fromJson(Map<String, dynamic> json) =>
      ExampleModel(list: json['list']);
}

At this time, Flutter can’t know the type of JSON data exactly, so it recognizes the string list data in JSON as List<dynamic>. When trying to assign this List<dynamic> data to List<String>?, the List<dynamic> is not a subtype of type List<String> error occurs as follows.

Flutter - List<dynamic> is not a subtype of type List<String> error

The error message is as follows.

Exception has occurred.
_TypeError (type 'List<dynamic>' is not a subtype of type 'List<String>?')

How to fix

To fix this problem, you need to convert the data type from List<dynamic> to List<String>?. Usually, we convert the type as follows.

factory ExampleModel.fromJson(Map<String, dynamic> json) =>
      ExampleModel(list: json['list'] as List<String>?);

However, you can still see the following error.

Flutter - List<dynamic> is not a subtype of type List<String> in type cast error

The error message is as follows.

Exception has occurred.
_TypeError (type 'List<dynamic>' is not a subtype of type 'List<String>? in type cast')

To fix this error, you need to convert the data type from List<dynamic> to List<String>? as follows.

factory ExampleModel.fromJson(Map<String, dynamic> json) => ExampleModel(
      list: (json['list'] as List?).map((item) => item as String).toList());

After modifying it like this, you can see that the problem is solved as follows.

Flutter - Fix List<dynamic> is not a subtype of type List<String> error

Completed

Done! We’ve seen how to fix the List<dynamic> is not a subtype of type List<String> error that occurs when receiving data through the http package and converting it to a model class. If you have the List<dynamic> is not a subtype of type List<String> error, please refer to this blog post and try to fix it.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts