Flutter 打印出所有hashMap值
想知道如果需要用dart语言编写的话,我如何循环通过级别列表来获取所有项目Flutter 打印出所有hashMap值,flutter,dart,hashmap,Flutter,Dart,Hashmap,想知道如果需要用dart语言编写的话,我如何循环通过级别列表来获取所有项目 var headers = { 'authorization': "Bearer" + " " + accessToken, "Accept": "application/json" }; var url = 'https://xxx'; var response = await http.get(
var headers = {
'authorization': "Bearer" + " " + accessToken,
"Accept": "application/json"
};
var url =
'https://xxx';
var response = await http.get(url, headers: headers);
var res = DataResponse.fromJson(json.decode(response.body));
for(var i in res.levels){
.... // I want print out the three items
}
数据响应
part 'data_response.g.dart';
@JsonSerializable()
class RFWIMasterDataResponse extends BaseResponse {
var levels = List<Level>();
DataResponse();
factory DataResponse.fromJson(Map<String, dynamic> json) =>
_$RFWIMasterDataResponseFromJson(json);
Map<String, dynamic> toJson() => _$DataResponseToJson(this);
}
一般来说,除了嵌套贴图之外,您没有其他东西。看这个 然而,在年,我为你的情况准备了一个测试
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
void main() {
final String stringMap =
'{"levels":[{"3":{"id":"3","level_no":"3"},"2":{"id":"2","level_no":"2"},"1":{"id":"1","level_no":"1"}}]}';
test("should have nested map on decode", () {
final map = json.decode(stringMap);
expect(map, isA<Map<dynamic, dynamic>>());
//levels is a list
expect(map["levels"], isA<List>());
//get the map if the list
expect(map["levels"][0], isA<Map<dynamic, dynamic>>());
map["levels"][0].forEach((key, value) {
print("key is 3 -> $key");
//get values by named index
print("values are ${value["id"]} and ${value["level_no"]}");
//you can also iterate over the values->values
value.forEach((innerKey, innerValue) {
//do something
});
});
});
}
导入'dart:convert';
进口“包装:颤振试验/颤振试验.dart”;
void main(){
最终字符串字符串映射=
{“级别”:[{“3”:{“id”:“3”,“级别编号”:“3”},“2”:{“id”:“2”,“级别编号”:“2”},“1”:{“id”:“1”,“级别编号”:“1”}]};
测试(“解码时应具有嵌套映射”,(){
final map=json.decode(stringMap);
expect(map,isA());
//级别是一个列表
expect(map[“levels”],isA());
//如果列表中没有地图,请获取地图
expect(map[“levels”][0],isA());
映射[“级别”][0]。forEach((键,值){
打印(“键为3->$key”);
//通过命名索引获取值
打印(“值为${value[“id”]}和${value[“level_no”]}”);
//您还可以迭代值->值
forEach((innerKey,innerValue){
//做点什么
});
});
});
}
无法将参数类型“Null Function(dynamic,dynamic)”分配给参数类型“void Function(dynamic)”
。我更正了我的答案,并为其编写了完整的测试用例。
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
void main() {
final String stringMap =
'{"levels":[{"3":{"id":"3","level_no":"3"},"2":{"id":"2","level_no":"2"},"1":{"id":"1","level_no":"1"}}]}';
test("should have nested map on decode", () {
final map = json.decode(stringMap);
expect(map, isA<Map<dynamic, dynamic>>());
//levels is a list
expect(map["levels"], isA<List>());
//get the map if the list
expect(map["levels"][0], isA<Map<dynamic, dynamic>>());
map["levels"][0].forEach((key, value) {
print("key is 3 -> $key");
//get values by named index
print("values are ${value["id"]} and ${value["level_no"]}");
//you can also iterate over the values->values
value.forEach((innerKey, innerValue) {
//do something
});
});
});
}