英文:
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
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<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];
}
答案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']),
);
并且将InsModel
的fromSnapshot
更改为:
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<InsEntity>? ins;
to
final InsEntity? ins;
and in the model
return UserModel(
uid: snapshot['uid'],
name: snapshot['name'],
ins: (snapshot['ins'] as List)
.map((e) => InsModel.fromSnapshot(e))
.toList(),
);
to
return UserModel(
uid: snapshot['uid'],
name: snapshot['name'],
ins: InsModel.fromSnapshot(snapshot['ins']),
);
and change the fromSnapshot
of InsModel
to
factory InsModel.fromSnapshot(Map<String, dynamic> snapshot ) {
return InsModel(
category: snapshot['category'],
type: snapshot['type'],
owned: snapshot['owned']);
}
(and maybe rename it to fromMap
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论