User Model Error:type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

huangapple go评论121阅读模式
英文:

User Model Error:type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

问题

I need help with this UserModel and this UserEntity when I tried to read json from firebase. I have problems with retrieve a map from firebase. I have this error: type '_Map<String, dynamic>' is not a subtype of type 'List' in type cast.
I am not sure how to do this correctly.
Can you help me?

Here is my code:

User Model:

class UserModel extends UserEntity {
  const UserModel({
    super.name,
    super.uid,
    super.ins,
  });

  factory UserModel.fromSnapshot(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map<String, dynamic>;

    return UserModel(
      uid: snapshot['uid'],
      name: snapshot['name'],
      ins: (snapshot['ins'] as List)
          .map((e) => InsModel.fromSnapshot(e))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() => {
    'uid': uid,
    'name': name,
    'ins': ins,
  };
}

class InsModel extends InsEntity {
  const InsModel({
    super.category,
    super.type,
    super.owned,
  });

  factory InsModel.fromSnapshot(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map<String, dynamic>;

    return InsModel(
      category: snapshot['category'],
      type: snapshot['type'],
      owned: snapshot['owned'],
    );
  }

  Map<String, dynamic> toJson() =>
  {'category': category, 'type': type, 'owned': owned};
}

User Entity:

class UserEntity extends Equatable {
  final String? uid;
  final String? name;
  final List<InsEntity>? ins;

  const UserEntity({
    this.uid,
    this.name,
    this.ins,
  });

  @override
  List<Object?> get props => [uid, name, ins];
}

class InsEntity extends Equatable {
  final String? category;
  final String? type;
  final bool? owned;

  const InsEntity({this.category, this.type, this.owned});

  @override
  List<Object?> get props => [category, type, owned];
}

Please let me know if you need further assistance.

英文:

I need help with this UserModel and this UserEntity when I tried to read json from firebase. I have problems with retrieve a map from firebase. I have this error:type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast.
I am not sure how to do this correctly
Can you help me?

Here is my code:

User Model:

class UserModel extends UserEntity {
  const UserModel(
      {super.name,
      super.uid,
      super.ins,
   });

  factory UserModel.fromSnapshot(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map&lt;String, dynamic&gt;;

    return UserModel(
        uid: snapshot[&#39;uid&#39;],
        name: snapshot[&#39;name&#39;],
        ins: (snapshot[&#39;ins&#39;] as List)
            .map((e) =&gt; InsModel.fromSnapshot(e))
            .toList(),
      );
  }

  Map&lt;String, dynamic&gt; toJson() =&gt; {
        &#39;uid&#39;: uid,
        &#39;name&#39;: name,
        &#39;ins&#39;: ins
      };
}

class InsModel extends InsEntity {
  const InsModel({super.category, super.type, super.owned});

  factory InsModel.fromSnapshot(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map&lt;String, dynamic&gt;;

    return InsModel(
        category: snapshot[&#39;category&#39;],
        type: snapshot[&#39;type&#39;],
        owned: snapshot[&#39;owned&#39;]);
  }

  Map&lt;String, dynamic&gt; toJson() =&gt;
      {&#39;category&#39;: category, &#39;type&#39;: type, &#39;owned&#39;: owned};
}

User Entity:

class UserEntity extends Equatable {
  final String? uid;
  final String? name;
  final List&lt;InsEntity&gt;? ins;

  const UserEntity(
      {
      this.uid,
      this.name,
      this.ins
     });

  @override
  List&lt;Object?&gt; get props =&gt; [
        uid,
        name,
        ins,
      ];
}

class InsEntity extends Equatable {
  final String? category;
  final String? type;
  final bool? owned;

  const InsEntity({this.category, this.type, this.owned});

  @override
  List&lt;Object?&gt; get props =&gt; [category, type, owned];
}

答案1

得分: 1

看起来ins是一个映射而不是列表。所以用户只有其中之一。然后你需要在实体中做以下更改:

final List<InsEntity>? ins;

改为:

final InsEntity? ins;

在模型中做以下更改:

return UserModel(
    uid: snapshot['uid'],
    name: snapshot['name'],
    ins: (snapshot['ins'] as List)
        .map((e) => InsModel.fromSnapshot(e))
        .toList(),
  );

改为:

return UserModel(
    uid: snapshot['uid'],
    name: snapshot['name'],
    ins: InsModel.fromSnapshot(snapshot['ins']),
  );

并且将InsModelfromSnapshot更改为:

factory InsModel.fromSnapshot(Map<String, dynamic> snapshot) {
  return InsModel(
      category: snapshot['category'],
      type: snapshot['type'],
      owned: snapshot['owned']);
}

(也许可以将其重命名为fromMap)。

英文:

It looks like ins is a map and not a list. So a user has only one of them. Then you need to change in the entity

final List&lt;InsEntity&gt;? ins;

to

final InsEntity? ins;

and in the model

return UserModel(
    uid: snapshot[&#39;uid&#39;],
    name: snapshot[&#39;name&#39;],
    ins: (snapshot[&#39;ins&#39;] as List)
        .map((e) =&gt; InsModel.fromSnapshot(e))
        .toList(),
  );

to

return UserModel(
    uid: snapshot[&#39;uid&#39;],
    name: snapshot[&#39;name&#39;],
    ins: InsModel.fromSnapshot(snapshot[&#39;ins&#39;]),
  );

and change the fromSnapshot of InsModel to

  factory InsModel.fromSnapshot(Map&lt;String, dynamic&gt; snapshot ) {
    return InsModel(
        category: snapshot[&#39;category&#39;],
        type: snapshot[&#39;type&#39;],
        owned: snapshot[&#39;owned&#39;]);
  }

(and maybe rename it to fromMap)

huangapple
  • 本文由 发表于 2023年6月13日 16:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定