首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型“List<dynamic>”不是类型“Map<String,dynamic>”的子类型

类型“List<dynamic>”不是类型“Map<String,dynamic>”的子类型
EN

Stack Overflow用户
提问于 2020-06-26 18:01:56
回答 1查看 122关注 0票数 0

我正在尝试点击一个laravel并在flutter中显示它。

代码语言:javascript
复制
[
    {
        "doctor_name": "abhishek",
        "username": "abhishek",
        "uid": "aLSb7ebMfsfAxybrwq21kXjkcJM2",
        "fees": 500,
        "speciality": "Oncologist"
    },
    {
        "username": "amanboi",
        "uid": "wpTQALmZd5Yr5BVQyblNstjet1A3",
        "fees": 500,
        "speciality": "Oncologist",
        "doctor_name": "aman"
    }
]

当我试图将它映射到我的模型时,我得到了错误。这是我的模型的样子

代码语言:javascript
复制
class Doctor {
  final String uid;
  final int fee;
  final String doctor_name;
  final String speciality;

  Doctor({this.uid, this.fee, this.doctor_name, this.speciality});

  factory Doctor.fromJson(Map<String, dynamic> json) {
    return Doctor(
      uid: json['userId'],
      fee: json['fee'],
      doctor_name: json['doctor_name'],
      speciality: json['speciality']
    );
  }
}

这是我的函数

代码语言:javascript
复制
Future<Doctor> doctorlist(String speciality ) async {
  final response = await http.post('http://192.168.0.101:8080/querysnapshot', body: {'speciality': speciality});
  print('got response successfully');
  if (response.statusCode == 200) {
    print(response.body);
    return Doctor.fromJson(json.decode(response.body));
  } else {
    throw Exception('Failed to load album');
  }
}

我收到以下错误:

代码语言:javascript
复制
type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
EN

回答 1

Stack Overflow用户

发布于 2020-06-26 18:06:24

您正在将从API获得的List传递到模型中。返回列表。

代码语言:javascript
复制
Future<List> doctorlist(String speciality) async {
  final response = await http.post('http://192.168.0.101:8080/querysnapshot', body: {'speciality': speciality});
  print('got response successfully');
  if (response.statusCode == 200) {
    print(response.body);
    return json.decode(response.body);
  } else {
    throw Exception('Failed to load album');
  }
}

在你的ListView中,你可以这样做

代码语言:javascript
复制
ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        Doctor doctor = Doctor.fromJson(doctorList[index]);
        return Text(doctor.name);
      },
    );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62592394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档