如何修复 Flutter 需要一个“Map<String, dynamic>”类型的值,但得到一个“List<dynamic>”类型的值
Posted
技术标签:
【中文标题】如何修复 Flutter 需要一个“Map<String, dynamic>”类型的值,但得到一个“List<dynamic>”类型的值【英文标题】:How to fix Flutter Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>' 【发布时间】:2021-06-12 07:27:35 【问题描述】:我正在开发一个使用 Flutter Web 和 RESTful API 作为后端的 Web 应用程序。 所以,我正在尝试从 api 中获取数据,使用 Flutter Models 对其进行序列化,然后返回结果。
问题是,我得到了这个结果
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
请帮我解决这个问题。
【问题讨论】:
你确定你只返回一个 MedicalRecordsManager 吗?...基于命名 getRecords().. 它应该是 Future>?.. 尝试打印出 json 响应并调试好 @Reign 我想返回的不仅仅是数据 您可以使这个过程变得更容易。请看这个答案。仅使用一个函数调用,您就可以得到最终结果。此方法已经过测试并在测试中涵盖。 ***.com/a/66632608/1737201 这些答案都不起作用吗? 【参考方案1】:你可以这样做
MedicalRecordsModel.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
【讨论】:
【参考方案2】:如下更改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
提供了地图列表,因此您的 json 地图是该列表的第一个元素。
【讨论】:
【参考方案3】:此代码将按您的预期工作:
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
也就是说,当response是一个对象列表时:
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 字符串并且想要转换结果(或其中的一部分),你可以使用以下方法:
如果解码的value
的类型是Map
:
final object = value.json((e) => Model.fromJson(e));
如果解码的value
的类型是List
:
final objects = value.json((e) => Model.fromJson(e));
【讨论】:
【参考方案4】:每个响应都以文本形式发送和接收,可以转换为
使用 dart 内置核心库 import 'dart:convert';
的地图格式。
所以请求的响应可以这样处理。
final res = await http.post(Uri.parse(url), body: json.encode(
'userId': uid,
'email': email,
),
head body: json.encode(
'userId': uid,
'email': email,
),
headers: 'Content-Type': 'application/json', 'token64': token);
这里json.encode()
用于从Map转换为String。
现在 res 变量包含响应,它也是一个字符串,可以像这样使用json.decode()
转换为 Map。
final data = json.decode(res);
在处理数据时,有时我们会发生错误,例如 Map is not a type of Map<String, String>
等。
这可以通过类型转换 res 来解决,像这样。
Map<String, String> notification = Map<String, String>.from(data['notification']);
我在 Youtube 的无聊的 Flutter 开发节目中看到了这些类型转换方法。
【讨论】:
以上是关于如何修复 Flutter 需要一个“Map<String, dynamic>”类型的值,但得到一个“List<dynamic>”类型的值的主要内容,如果未能解决你的问题,请参考以下文章
如何过滤 List<Map<String, dynamic>> 以在 Flutter 中获取值?
如何在 Flutter 上向 List<Map<String, String>> 输入第三个字符串?
Dart/Flutter - 如何删除 List 中 Map 元素的键值对?
如何在 Flutter 的 map<String, dynamic> 中转换 http.MultipartRequest POST 方法的响应?