Dart 映射 - 类型 'String' 不是 'index' 的类型 'int' 的子类型

Dart map - type 'String' is not a subtype of type 'int' of 'index'

我正在按照一些 youtube 教程制作一个应用程序来学习 flutter。

我在应用程序视图中显示搜索结果时遇到问题。我能够从节点后端查询和获取数据,但是在将 json 映射到模型时出现此错误。

我从api得到的数据是这样的:

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

错误出现在 model.dart 文件的 profileName 映射行中。

class AccountModel {
  String userId;
  String userEmail;
  String? userPassword;

  AccountModel({
    required this.userId,
    required this.userEmail,
    this.userPassword,
  });
}

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

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

  factory ProfileModel.fromMap({required Map<String, dynamic> map}) {
    print(map);
    return ProfileModel(
      profileName: map['profile']['profileName'],
      profileImage: map['profile']['profileImage'] ?? "default",
      profileBio: map['profile']['profileBio'],
      accountModel: AccountModel(
        userId: map['id'],
        userEmail: map['userEmail'],
        userPassword: map['userPassword'],
      ),
    );
  }

  factory ProfileModel.fromMapFollowerData(
      {required Map<String, dynamic> map}) {
    return ProfileModel(
      profileName: map['profileName'],
      profileImage: map['profileImage'] ?? "default",
      profileBio: map['profileBio'],
      accountModel: AccountModel(
        userId: map['userId'],
        userEmail: map['userEmail'],
      ),
    );
  }
}

也许我理解不正确,但我认为因为配置文件数据在 [] 中,所以我需要指定索引。

如何纠正这个错误?

编辑:按照 Majid 的指导更新后的新模型:

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'],
      );
    }
  }
}

由于使用该模型的其他页面中的某些功能的设置方式,我遇到了一些错误,因此我进行了一些更改。我在 account/profile 创建、post 创建页面中没有错误,但搜索页面仍然有错误。现在列表的错误是:type 'List<dynamic>' is not a subtype of type 'List<ProfileModel>?'.

错误是帐户模型映射配置文件模型并将其添加到列表。我尝试将列表键入如下:.toList<ProfileModel>(),,我确定这是愚蠢的,因为它显示 NoSuchMethodError (NoSuchMethodError: Class 'MappedListIterable<dynamic, dynamic>' has no instance method 'toList' with matching arguments. Receiver: Instance of 'MappedListIterable<dynamic, dynamic>' Tried calling: toList<ProfileModel>() Found: toList({bool growable}) => List<X0>)

这里的问题是 profileMap<String, String>ListArray 这是你的例子

...
profile: [

{profileName: <profile_name_string>, 
    profileImage: <image_url_string>, 
    profileBio: <profile_bio_string>}
]

这意味着当您想要访问它时,您必须遍历 List 以将它们全部转换。

这意味着您模型中的这一行将变为

 profileName: map['profile'][0]['profileName'],
 profileImage: map['profile'][0]['profileImage'] ?? "default",
 profileBio: map['profile'][0]['profileBio'],

但是,这有一个潜在的问题。因为你可能有一个 List 没有成员,这意味着 map['profile'][0] 可能是空的或 null 另一个问题是 profile 有多个成员的时间怎么样你有 map['profile][0]map['profile'][1] 等等?在这种情况下,您缺少一些成员。

如果你确定你总是有一个成员并且在你的 profile List 中只有一个成员,你可以坚持只使用 0 索引,但是如果你想做得更好,你应该这样做:

class UserAccountModel {
  UserAccountModel({
    this.id,
    this.userEmail,
    this.profile,
  });

  final String? id;
  final String? userEmail;
  final List<ProfileModel>? profile;

    factory UserAccountModel.fromJson(Map<String, dynamic> map) {
    final profile = map['profile'] as List<dynamic> ;
    return UserAccountModel(
      id: map['id'],
      userEmail: map['userEmail'],
      profile: profile.map((profileJson) => ProfileModel.fromJson(profileJson)).toList(),
    );
  }
}

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

  final String? profileName;
  final String? profileImage;
  final String? profileBio;

  factory ProfileModel.fromJson(Map<String, String> map) {
    return ProfileModel(
      profileName: map['profileName'],
      profileImage: map['profileImage'] ?? "default",
      profileBio: map['profileBio'],
    );
  }
}

我强烈建议您使用 json_serializable 以避免任何错误。