【发布时间】:2020-06-10 15:56:07
【问题描述】:
使用How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter 中的示例,我创建了以下工作示例:
main.dart
import 'package:flutter/material.dart';
import 'dart:convert';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String jsonData =
'[{"id":"e20c","name":"Apples","type":"fruit"},{"id":"a24e","name":"Oranges","type":"fruit"},{"id":"f2a0","name":"Bananas","type":"fruit"}]';
List<FruitResponse> _fruitResponse = [];
String selectedName;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final json = JsonDecoder().convert(jsonData);
_fruitResponse = (json)
.map<FruitResponse>((item) => FruitResponse.fromJson(item))
.toList();
return MaterialApp(
title: 'Pick Fruit',
home: Scaffold(
appBar: AppBar(
title: Text("Pick Fruit"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: Text("Select Fruit"),
value: selectedName,
isDense: true,
onChanged: (String newValue) {
setState(() {
selectedName = newValue;
});
print(selectedName);
},
items: _fruitResponse.map((FruitResponse map) {
return DropdownMenuItem<String>(
value: map.nameDescription,
child: Text(map.nameDescription),
);
}).toList(),
)),
],
),
),
));
}
}
class FruitResponse {
final String nameid;
final String nameDescription;
FruitResponse({this.nameid, this.nameDescription});
factory FruitResponse.fromJson(Map<String, dynamic> json) {
return new FruitResponse(nameid: json['id'], nameDescription: json['name']);
}
}
但是,我的 JSON 数据将是
{"objects":[{"id":"e20c","name":"Apples","type":"fruit"},{"id":"a24e","name":"Oranges","type":"fruit"},{"id":"f2a0","name":"Bananas","type":"fruit"}],"from":1,"to":3,"total":3}
我已经使用https://app.quicktype.io/ 生成以下内容
FruitResponse fruitResponseFromJson(String str) => FruitResponse.fromJson(json.decode(str));
String fruitResponseToJson(FruitResponse data) => json.encode(data.toJson());
class FruitResponse {
List<Object> objects;
int from;
int to;
int total;
FruitResponse({
this.objects,
this.from,
this.to,
this.total,
});
factory FruitResponse.fromJson(Map<String, dynamic> json) => FruitResponse(
objects: List<Object>.from(json["objects"].map((x) => Object.fromJson(x))),
from: json["from"],
to: json["to"],
total: json["total"],
);
Map<String, dynamic> toJson() => {
"objects": List<dynamic>.from(objects.map((x) => x.toJson())),
"from": from,
"to": to,
"total": total,
};
}
class Object {
String id;
String name;
String type;
Object({
this.id,
this.name,
this.type,
});
factory Object.fromJson(Map<String, dynamic> json) => Object(
id: json["id"],
name: json["name"],
type: json["type"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"type": type,
};
}
当我用更新的类 FruitResponse 替换类 FruitResponse 并对项目映射进行更改时,我得到一个错误。
类'_InternalLinkedHashMap'没有匹配的实例方法'map'
这里是 DartPad 中的工作示例https://dartpad.dev/b54d896aa35c159cd1749d5c67db7d52
此处 DartPad 中的非工作示例 https://dartpad.dev/0413fb4bb7944ccd378b9eabf4e88ff3
我认为问题只是从 json 数据中正确获取 List<Object> 名称并在 DropDownButton 项目值中使用它。我知道 map.objects.toString() 不正确,但我不知道该放什么,或者我是否缺少 _fruitResponse 的内容。
提前感谢您的帮助。我正在努力理解映射 JSON 响应列表数据。
【问题讨论】: