【问题标题】:How to Handle "" type 'bool' is not a subtype of type 'Map<String, dynamic>' "" errror in flutter?如何处理“”类型'bool'不是'Map<String,dynamic>'类型的子类型“”颤振错误?
【发布时间】:2021-04-10 05:42:46
【问题描述】:

我有这个错误的问题: “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 也得到字符串和响应布尔值。 我的洞错误:

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>

【问题讨论】:

  • 错误信息很清楚。 LoginUserModel.fromJson 需要 Map&lt;String,dynamic&gt; 的输入类型,但您已为其提供了 boolean 值。
  • 你能帮我举个例子或提供细节吗?谢谢爸爸。
  • 这不是导致问题的代码部分,当您调用 toJson 或 fromJson 时,代码中有其他内容。发布那部分
  • 这可以是您的构造函数的有效输入。 Map&lt;String, dynamic&gt; userData = {'id': 1};。将其传递给您的构造函数。 LoginUserModel myModel = LoginUserModel.fromJson(userData);
  • 还是不明白!我做了你的代码,仍然有问题

标签: android ios flutter dart


【解决方案1】:

实例化部分代码:

                              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');
                                  }
                                },

【讨论】:

  • 您可以打印您的回复正文吗?
  • 我建议删除你的 api 链接。
【解决方案2】:

我找到了解决方案。 就我而言,您不需要模型,因为我对 json 的响应不是对象,它只是一个布尔值。

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 2020-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2020-05-24
    • 2019-08-07
    • 2021-07-21
    • 2021-12-10
    相关资源
    最近更新 更多