Flutter Firestore with Streams getting type '_JsonQueryDocumentSnapshot' is not a subtype' 错误?
Flutter Firestore with Streams getting type '_JsonQueryDocumentSnapshot' is not a subtype' error?
我在尝试使用 Firestore 和 Stream Provider 时遇到以下错误
type '_JsonQueryDocumentSnapshot' is not a subtype of type 'Map<String, dynamic>' in type cast
这是我的根页面
ChangeNotifierProxyProvider<AuthProvider, DogProvider>(
update: (context, value, previous) => DogProvider(), // ..getDogList()
create: (_) => DogProvider(), //..getDogList()
),
StreamProvider<Object>(
create: (context) =>
DogFirestoreService().getDogs('GeVnAbdq9BWs1STbytlAU65qkbc2'),
initialData: 10,
child: const DogsListScreen(),
),
我的 Firestore 服务获取流
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(),
);
}
模特
@JsonSerializable(explicitToJson: true)
class DogModel {
String? dogImage;
String dogName;
DogBreed? breedInfo;
@JsonKey(fromJson: dateTimeFromTimestamp, toJson: dateTimeAsIs)
DateTime? dogBirthday;
double? dogWeight;
DogWeight? weightType;
DogGender? dogGender;
List<String>? dogType;
DogStatus? dogStatus;
bool? registered;
String? dogChipId;
@JsonKey(fromJson: dateTimeFromTimestamp, toJson: dateTimeAsIs)
DateTime? createdAt;
DogStats? dogStats;
@JsonKey(ignore: true)
String? id;
String? userId;
DogModel({
required this.dogImage,
required this.dogName,
required this.breedInfo,
required this.dogBirthday,
required this.dogWeight,
required this.weightType,
required this.dogGender,
required this.dogType,
required this.dogStatus,
required this.registered,
this.dogStats,
this.dogChipId,
this.createdAt,
this.userId,
this.id,
});
factory DogModel.fromFire(QueryDocumentSnapshot snapshot) {
return DogModel.fromJson(snapshot as Map<String, dynamic>);
}
// JsonSerializable constructor
factory DogModel.fromJson(Map<String, dynamic> json) =>
_$DogModelFromJson(json);
// Serialization
Map<String, dynamic> toJson() => _$DogModelToJson(this);
}
并在小部件中
Widget build(BuildContext context) {
final test = context.watch<Object>();
print(test);
正如我们在另一个 中讨论的那样,您需要按如下方式从文档中提取数据:
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromJson(document.data()) // <~~ here
.toList(),
);
如您所见,您应该调用 document.data()
(而不是 document.data
,这是我之前的错误)。
我在尝试使用 Firestore 和 Stream Provider 时遇到以下错误
type '_JsonQueryDocumentSnapshot' is not a subtype of type 'Map<String, dynamic>' in type cast
这是我的根页面
ChangeNotifierProxyProvider<AuthProvider, DogProvider>(
update: (context, value, previous) => DogProvider(), // ..getDogList()
create: (_) => DogProvider(), //..getDogList()
),
StreamProvider<Object>(
create: (context) =>
DogFirestoreService().getDogs('GeVnAbdq9BWs1STbytlAU65qkbc2'),
initialData: 10,
child: const DogsListScreen(),
),
我的 Firestore 服务获取流
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(),
);
}
模特
@JsonSerializable(explicitToJson: true)
class DogModel {
String? dogImage;
String dogName;
DogBreed? breedInfo;
@JsonKey(fromJson: dateTimeFromTimestamp, toJson: dateTimeAsIs)
DateTime? dogBirthday;
double? dogWeight;
DogWeight? weightType;
DogGender? dogGender;
List<String>? dogType;
DogStatus? dogStatus;
bool? registered;
String? dogChipId;
@JsonKey(fromJson: dateTimeFromTimestamp, toJson: dateTimeAsIs)
DateTime? createdAt;
DogStats? dogStats;
@JsonKey(ignore: true)
String? id;
String? userId;
DogModel({
required this.dogImage,
required this.dogName,
required this.breedInfo,
required this.dogBirthday,
required this.dogWeight,
required this.weightType,
required this.dogGender,
required this.dogType,
required this.dogStatus,
required this.registered,
this.dogStats,
this.dogChipId,
this.createdAt,
this.userId,
this.id,
});
factory DogModel.fromFire(QueryDocumentSnapshot snapshot) {
return DogModel.fromJson(snapshot as Map<String, dynamic>);
}
// JsonSerializable constructor
factory DogModel.fromJson(Map<String, dynamic> json) =>
_$DogModelFromJson(json);
// Serialization
Map<String, dynamic> toJson() => _$DogModelToJson(this);
}
并在小部件中
Widget build(BuildContext context) {
final test = context.watch<Object>();
print(test);
正如我们在另一个
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromJson(document.data()) // <~~ here
.toList(),
);
如您所见,您应该调用 document.data()
(而不是 document.data
,这是我之前的错误)。