我收到这个错误地图
我在尝试使用提供程序时收到此错误 Map<String, dynamic> is not a subtype of type String
,并且我不知道错误出在哪里。
看看我的模型
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
class User {
final String id;
final String name;
final String email;
final int phone;
final String? image;
final String location;
final String sex;
final String password;
final String? age;
final String? preferredLocation;
final List<dynamic>? education;
final List<dynamic>? workExperience;
final String role;
final List<dynamic>? applications;
final List<dynamic>? favoriteJobs;
User({
required this.id,
required this.name,
required this.email,
required this.phone,
this.image,
required this.location,
required this.sex,
required this.password,
this.age,
this.preferredLocation,
this.education,
this.workExperience,
required this.role,
this.applications,
this.favoriteJobs,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'name': name,
'email': email,
'phone': phone,
'image': image,
'location': location,
'sex': sex,
'password': password,
'age': age,
'preferredLocation': preferredLocation,
'education': education,
'workExperience': workExperience,
'role': role,
'applications': applications,
'favoriteJobs': favoriteJobs,
};
}
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['_id'] ?? "",
name: map['name'] ?? "",
email: map['email'] ?? "",
phone: map['phone'] ?? 0,
image: map['image'] ?? "",
location: map['location'] ?? "",
sex: map['sex'] ?? "",
password: map['password'] ?? "",
age: map['age'] ?? "",
preferredLocation: map['preferredLocation'] ?? "",
education: map['education'] != null
? List<Map<String, dynamic>>.from(
(map['education']?.map((x) => Map<String, dynamic>.from(x))))
: [],
workExperience: map['workExperience'] != null
? List<Map<String, dynamic>>.from(
(map['workExperience']?.map((x) => Map<String, dynamic>.from(x))))
: [],
role: map['role'] ?? "job_seeker",
applications: map['applications'] != null
? List<Map<String, dynamic>>.from(
(map['applications']?.map((x) => Map<String, dynamic>.from(x))))
: [],
favoriteJobs: map['favoriteJobs'] != null
? List<Map<String, dynamic>>.from(
(map['favoriteJobs']?.map((x) => Map<String, dynamic>.from(x))))
: [],
);
}
String toJson() => json.encode(toMap());
factory User.fromJson(String source) =>
User.fromMap(json.decode(source) as Map<String, dynamic>);
}
我的提供商看起来像这样
import 'package:flutter/material.dart';
import 'package:job_finder_app/models/usermodel.dart';
class AllusersProvider extends ChangeNotifier {
List<User> _users = [];
List<User> get users => _users;
` void setUsers(List<User> users) {
` _users = users;
notifyListeners();
}
void addUser(User user) {
_users.add(user);
notifyListeners();
}
void removeUser(User user) {
_users.remove(user);
notifyListeners();
}
}
我打印了响应,并以 json 格式获取响应,它看起来像数据,但我无法在 Ui 中使用它,因为我得到了 Map<String, dynamic> is not a subtype of type String
错误 'FirstType' 不是 'SecondType' 类型的子类型 意味着 dart 试图将 FirstType 为 SecondType 。在你的情况 _Map<String, dynamic>
下 String
.
错误消息中类型名称中 \'Map\' 前面的 下划线 \'_\' _Map
是用于存储解析的 json 对象的基础类型之一 json.decode
(可能是 _JsonMap
其他内部(下划线前缀)类型)。然后您尝试将该 _Map<String, dynamic>
对象 String
内部 User.fromMap
。请检查您期望在 api 响应中为字符串但却是对象的所有字段(我猜它是 \'location\' 或 \'preferredLocation\' 字段)。
我打印了响应,并以 json 格式获取响应,它看起来是数据
使用 API 测试工具(如 Postman)来全面检查响应或打印对象内每个值的类型, json.decode(source)
以查看哪些值未对齐:
json.decode(source).forEach((k, v) {
print('{ key: $k, value type: ${v.runtimeType}}');
});