我遇到此错误的问题:
“bool”类型不是“Map
class LoginUserModel {
final id;
LoginUserModel({@required this.id});
factory LoginUserModel.fromJson(Map<String, dynamic> json) {
return LoginUserModel(
id: json['id'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
return data;
}
}
我的 id 也得到 String 和 reponse 布尔值。 我的洞错误:
Performing hot restart...
Syncing files to device iPhone 12 mini...
Restarted application in 4,667ms.
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type 'bool' is not a subtype of type 'Map<String, dynamic>'
#0 LoginPage.build.<anonymous closure>.fetchLogin (package:amlake_shaar_main1/loginUser.dart:103:74)
<asynchronous suspension>
答案 0 :(得分:0)
实例化部分代码:
final ID = _controller.text.toString();
final url = "https://api.shaarapp.ir/api/User/CheckHasUser?id=$ID";
Future<LoginUserModel> fetchLogin() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return LoginUserModel.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed');
}
}
FutureBuilder<LoginUserModel>(
future: fetchLogin(),
builder: (context, snapshot){
if(ID == snapshot.data.id.toString() && snapshot.data.id.toString() == true){
print(url);
}else{
print('not');
}
},
答案 1 :(得分:0)
我找到了解决方案。 就我而言,您不需要模型,因为我对 json 的响应不是对象,它只是一个布尔值。