Flutter Firestore 流错误 "as required by the closure's context."
Flutter Firestore Stream error "as required by the closure's context."
我收到以下错误,不知道为什么 The return type 'List<DogModel>' isn't a 'DogModel', as required by the closure's context.
这是我获取流的 Firestore 服务文件
Stream<DogModel> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(),
);
}
这是我的模型
part 'dog_model.g.dart';
enum DogGender {
male,
female,
}
enum DogWeight {
kg,
lbs,
}
enum DogStatus {
active,
training,
injured,
retired,
}
@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);
}
如果我这样做 Stream<Object>
没问题
getDogs
函数体返回 List<DogModel>
但函数定义是 Stream<DogModel>
。因此将定义更改为 Stream<List<DogModel>>
如下:
// fix this ↓
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(), // <~~~~ you're returning a list here
);
您已将 return 类型的方法 getDogs()
定义为 Stream<DogModel>
而您的方法 return 是 Stream<List<DogModel>>
,即 return List
个 DogModel
秒。所以你的 getDogs()
方法声明应该是这样的:
Stream<<List<DogModel>> getDogs(String uid) {
...
}
我收到以下错误,不知道为什么 The return type 'List<DogModel>' isn't a 'DogModel', as required by the closure's context.
这是我获取流的 Firestore 服务文件
Stream<DogModel> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(),
);
}
这是我的模型
part 'dog_model.g.dart';
enum DogGender {
male,
female,
}
enum DogWeight {
kg,
lbs,
}
enum DogStatus {
active,
training,
injured,
retired,
}
@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);
}
如果我这样做 Stream<Object>
没问题
getDogs
函数体返回 List<DogModel>
但函数定义是 Stream<DogModel>
。因此将定义更改为 Stream<List<DogModel>>
如下:
// fix this ↓
Stream<List<DogModel>> getDogs(String uid) {
return dogCollection.where('userId', isEqualTo: uid).snapshots().map(
(snapshot) => snapshot.docs
.map((document) => DogModel.fromFire(document))
.toList(), // <~~~~ you're returning a list here
);
您已将 return 类型的方法 getDogs()
定义为 Stream<DogModel>
而您的方法 return 是 Stream<List<DogModel>>
,即 return List
个 DogModel
秒。所以你的 getDogs()
方法声明应该是这样的:
Stream<<List<DogModel>> getDogs(String uid) {
...
}