我正在开发一个web应用程序,使用flutraweb和restfulapi作为后端。因此,我尝试从api中获取数据,使用颤振模型对其进行序列化,然后返回结果。
问题是,我得到了这个结果
Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>'
如何解决这个问题?
这是我的颤动代码:
模型
// 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
}
请帮我拿这个。
此代码将按照您的预期工作:
import 'package:json_helpers/json_helpers.dart';
void main() {
// responseBody is the same response.body
// When response is a list of objects
final list = responseBody1.jsonList((e) => MedicalRecordsModel.fromJson(e));
var obj = list[0];
print(obj.category);
print(obj.fileName);
// When response is an object
obj = responseBody2.json((e) => MedicalRecordsModel.fromJson(e));
print(obj.category);
print(obj.fileName);
}
final responseBody1 = '''
[
{
"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
}
]''';
final responseBody2 = '''
{
"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
}''';
class MedicalRecordsModel {
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;
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,
});
factory MedicalRecordsModel.fromJson(Map<String, dynamic> json) {
return MedicalRecordsModel(
id: json['id'] as String,
category: json['category'] as String,
fileName: json['fileName'] as String,
dateTimestamp: json['dateTimestamp'] as String,
description: json['description'] as String,
upload: json['upload'] as String,
patientName: json['patientName'] as String,
age: json['age'] as String,
address: json['address'] as String,
userId: json['userId'] as String,
patientId: json['patientId'] as int,
isActive: json['isActive'] as bool,
);
}
}
输出:
DOCUMENT
Check Up
DOCUMENT
Check Up
也就是说,当响应是对象列表时:
final list = response.body.jsonList((e) => MedicalRecordsModel.fromJson(e));
当响应是对象时:
final object = response.body.json((e) => MedicalRecordsModel.fromJson(e));
如果你不知道结果是什么,那么你可以尝试两种方法。
response.body.json((e) => Model.fromJson(e));
response.body.jsonList((e) => Model.fromJson(e));
如果您已经解码了JSON字符串并希望转换结果(或部分结果),则可以使用以下方法:
如果解码的值的类型为
Map
:
final object = value.json((e) => Model.fromJson(e));
如果解码的
值
的类型为List
:
final objects = value.json((e) => Model.fromJson(e));
更改getRecords
如下所示
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
给出了映射列表,因此您的json映射是该列表的第一个元素。
你可以这样做
MedicalRecordsModel.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
我是新来的。当我映射响应体它给错误。 应为“Map”类型的值
大家好,我正在制作一个菜谱应用程序,它显示食物的名称、图像和ingredants,所以我制作了一个简单的主页,制作了requst和一个类,该类提供了我从spooncular使用的这个url的数据。通用域名格式: https://api.spoonacular.com/recipes/complexSearch?apiKey=8fe91444f092411fa6011b71fd6e582d 这是主课
在代码中,代码以实时(流)方式从cloud firestore获取数据,并使用StreamBuilder将其显示在数据表小部件中,但当我运行代码时,正如我上面所问的,它给出了错误。 _listofRows方法代码在这里 这是一个产品集合的文件示例在fireft数据库。(
我使用超文本传输协议:^0.13.3,这是我的简单代码,当我运行此代码时,我得到了错误:"期望类型'Map的值
我正在开发一个依靠API REST调用的flutter应用程序。来自API的响应有点复杂。当调用我的API(例如:api/产品)时,我可以从日志中看到响应:但是我有这个错误: 类型列表 StackTrace: [更新]:退货产品响应。fromJson(response)而不是response ProductsRespository: ProductsBloc: 回应: ApiProvider: 模
我正在学习飞镖和颤振。现在,我尝试将JSON作为一种持久性方法。我犯了很多错误,都是关于类型和内容的。这是我遇到的最新错误: