Firebase 参数类型';对象?';can';不能分配给参数类型';地图<;字符串,动态>';
我正在尝试使用小部件从Firestore中的文档检索数据。我以前用过这个,我想是用了另一个版本的飞镖或颤振,它很管用。现在,当我使用Firebase 参数类型';对象?';can';不能分配给参数类型';地图<;字符串,动态>';,firebase,flutter,dart,Firebase,Flutter,Dart,我正在尝试使用小部件从Firestore中的文档检索数据。我以前用过这个,我想是用了另一个版本的飞镖或颤振,它很管用。现在,当我使用(doc.data()) 无法将参数类型“Object”分配给参数类型“Map” 这是密码 DriverProvider driverProvider; void getDriverInfo() { Stream<DocumentSnapshot> driverStream = driverProvider!.getbyIdSt
(doc.data())
无法将参数类型“Object”分配给参数类型“Map”
这是密码
DriverProvider driverProvider;
void getDriverInfo() {
Stream<DocumentSnapshot> driverStream =
driverProvider!.getbyIdStream(FirebaseAuth.instance.currentUser!.uid);
driverStream.listen((DocumentSnapshot doc) {
driver = Driver.fromJson(doc.data());
});
}
这是我的Json模型
import 'dart:convert';
Driver driverFromJson(String str) => Driver.fromJson(json.decode(str));
String driverToJson(Driver data) => json.encode(data.toJson());
class Driver {
Driver({
required this.id,
required this.username,
required this.email,
required this.truck,
required this.carrier,
required this.insurance,
});
String id;
String username;
String email;
String truck;
String carrier;
String insurance;
factory Driver.fromJson(Map<String, dynamic> json) => Driver(
id: json["id"],
username: json["username"],
email: json["email"],
truck: json["truck"],
carrier: json["carrier"],
insurance: json["insurance"],
);
Map<String, dynamic> toJson() => {
"id": id,
"username": username,
"email": email,
"truck": truck,
"carrier": carrier,
"insurance": insurance,
};
}
谢谢大家的帮助。此错误是对
cloud\u firestore
API进行新更改的结果。
您需要指定从DocumentSnapshot
更新此行:
driver=driver.fromJson(doc.data());
为此:
driver=driver.fromJson(doc.data()作为映射);
查看指南。除了Victor的答案,您还可以执行以下操作:
stream您可以添加您的fromJson
方法并向我们显示错误所在的行吗?感谢您的回答。我在初始问题中添加了Json和带有错误的行。您在哪里定义驱动程序变量?你能试着打印出doc.data()
看看你得到了什么吗?我需要像这样在用户配置文件容器中从我的页面控制器调用变量(子:Text(mapController.driver!.username,
我真的无法打印doc.data())因为这是我需要帮助的错误,代码编辑器不允许我编译。但是,我知道它包含文档中的字段
Stream<DocumentSnapshot> getbyIdStream(String id) {
return _ref!.doc(id).snapshots(includeMetadataChanges: true);
}
import 'dart:convert';
Driver driverFromJson(String str) => Driver.fromJson(json.decode(str));
String driverToJson(Driver data) => json.encode(data.toJson());
class Driver {
Driver({
required this.id,
required this.username,
required this.email,
required this.truck,
required this.carrier,
required this.insurance,
});
String id;
String username;
String email;
String truck;
String carrier;
String insurance;
factory Driver.fromJson(Map<String, dynamic> json) => Driver(
id: json["id"],
username: json["username"],
email: json["email"],
truck: json["truck"],
carrier: json["carrier"],
insurance: json["insurance"],
);
Map<String, dynamic> toJson() => {
"id": id,
"username": username,
"email": email,
"truck": truck,
"carrier": carrier,
"insurance": insurance,
};
}
driver = Driver.fromJson(doc.data());