gpt4 book ai didi

flutter - 如何修复 Flutter 期望类型为 'Map' 的值,但得到类型为 'List' 的值

转载 作者:行者123 更新时间:2023-12-03 18:52:33 25 4
gpt4 key购买 nike

我正在开发一个使用 Flutter Web 和 RESTful API 作为后端的 Web 应用程序。
所以,我正在尝试从 api 中获取数据,使用 Flutter 模型对其进行序列化,然后返回结果。
问题是,我得到了这个结果

Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>'
如何解决这个问题?
这是我的 flutter 代码:
型号
// To parse this JSON data, do
//
// final medicalRecordsModel = medicalRecordsModelFromJson(jsonString);

import 'dart:convert';

class MedicalRecordsModel {
MedicalRecordsModel({
this.id,
this.category,
this.fileName,
this.dateTimestamp,
this.description,
this.upload,
this.patientName,
this.age,
this.address,
this.userId,
this.patientId,
this.isActive,
});

final String id;
final String category;
final String fileName;
final String dateTimestamp;
final String description;
final String upload;
final String patientName;
final String age;
final String address;
final dynamic userId;
final int patientId;
final bool isActive;

factory MedicalRecordsModel.fromJson(Map<String, dynamic> json) {
return MedicalRecordsModel(
id: json["id"],
category: json["category"],
fileName: json["fileName"],
dateTimestamp: json["dateTimestamp"],
description: json["description"],
upload: json["upload"],
patientName: json["patientName"],
age: json["age"],
address: json["address"],
userId: json["userId"],
patientId: json["patientId"],
isActive: json["isActive"],
);
}
}

API连接
import 'dart:convert';
import 'dart:developer';
import 'dart:async';
import 'package:app/src/constants/medical_records.dart';
import 'package:app/src/models/medical_records/medical_records.dart';
import 'package:app/src/pages/Medical-Records/medical_record.dart';
import 'package:http/http.dart' as http;

class MedicalRecordsManager {
var client = http.Client();
var url = ConstantMedicalRecords.medical_records_api;

Future<MedicalRecordsModel> getRecords() async {
var url = ConstantMedicalRecords.medical_records_api;
log('$url');
try {
final response = await client.get(url);
if (response.statusCode == 200) {
return MedicalRecordsModel.fromJson(jsonDecode(response.body));
// print(recordsModel);
}
} catch (Exception) {
print(Exception);
print("Error occured");
}
}
}


这是我想要获取的 JSON 数据
 {
"id": "103",
"category": "DOCUMENT",
"fileName": "Check Up",
"dateTimestamp": "2021-02-1012:59:46",
"description": "string",
"upload": "String",
"patientName": "1",
"age": "25",
"address": "Earth",
"userId": null,
"patientId": 12,
"isActive": true
}
请帮我解决这个问题。

最佳答案

更改 getRecord如下

Future<MedicalRecordsModel> getRecords() async {
var url = ConstantMedicalRecords.medical_records_api;
log('$url');
try {
final response = await client.get(url);
if (response.statusCode == 200) {
return MedicalRecordsModel.fromJson(jsonDecode(response.body)[0]);
// print(recordsModel);
}
} catch (Exception) {
print(Exception);
print("Error occured");
}
}
我想 jsonDecode给出 map 列表,因此您的 json map 是该列表的第一个元素。

关于flutter - 如何修复 Flutter 期望类型为 'Map<String, dynamic>' 的值,但得到类型为 'List<dynamic>' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66631068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com