Home > Blockchain >  Flutter - type 'List<dynamic>' is not a subtype of type 'List<Model>?
Flutter - type 'List<dynamic>' is not a subtype of type 'List<Model>?

Time:03-18

I'm learning flutter by making an app following some youtube tutorials. I'm trying to make a listview of search results. I'm able to query and get data from node backend but there's this error while mapping the json to model.

The data I'm getting from api is like this:

{id: <uuid>, 
userEmail: <email_string>, 
profile: [{profileName: <profile_name_string>, 
    profileImage: <image_url_string>, 
    profileBio: <profile_bio_string>}]
}

With the new model class I made following an answer here I'm able to get profile model separately but when i try to get account model with all profiles I'm getting the error:type 'List<dynamic>' is not a subtype of type 'List<ProfileModel>?'. The model class is:

class AccountModel {
  String userId;
  String userEmail;
  String? userPassword;
  final List<ProfileModel>? profile;

  AccountModel({
    required this.userId,
    required this.userEmail,
    this.userPassword,
    this.profile,
  });
  factory AccountModel.fromJson({required Map<String, dynamic> map}) {
    return AccountModel(
      userId: map['id'],
      userEmail: map['userEmail'],
      userPassword: map['userPassword'],
      profile: map['profile']
          .map((profileJson) => ProfileModel.fromJson(profileJson))
          .toList(),
    );
  }
}

class ProfileModel {
  String profileName;
  String profileImage;
  String? profileBio;

  ProfileModel({
    required this.profileName,
    required this.profileImage,
    this.profileBio,
  });

  factory ProfileModel.fromJson(profileJson, {Map<String, dynamic>? map}) {
    if (map != null) {
      return ProfileModel(
        profileName: map['profileName'],
        profileImage: map['profileImage'] ?? "default",
        profileBio: map['profileBio'],
      );
    } else {
      return ProfileModel(
        profileName: profileJson['profileName'],
        profileImage: profileJson['profileImage'] ?? "default",
        profileBio: profileJson['profileBio'],
      );
    }
  }
}

How to make the list work?

CodePudding user response:

You can use List.from() in this case.

profile: map['profile'] != null
      ? List<ProfileModel>.from(
          map['profile']?.map((p) => ProfileModel.fromJson(p)))
      : null)

We are using fromMap here on ProfileModel, you can simplify just separation while both are same on ProfileModel.

More about List and List.from.

CodePudding user response:

When you declared the list here as

final List<ProfileModel>? profile;

It expects the list to have only ProfileModels as ListItem even though with "?". The way to solve it is either declared a list without generic ProfileModel :

  1. final List? profile;

Or to typecast the item you're pushing as ProfileModel. 2. profile: map['profile'] .map((profileJson) => ProfileModel.fromJson(profileJson) as ProfileModel) .toList(),

I don't know the output structures and such so try to experiment with typecasting if the above code doesn't work. May be typecasting after toList() method as List can work too.

  • Related