在我的flutter应用程序中,我需要创建3下拉按钮。我有本地的Json文件,其中包含不同国家/地区的公司列表。
查询:
我知道如何加载本地json。我也知道如何制作下拉按钮。但是我不知道如何根据我上面试图解释的3个查询来加载所有3个下拉按钮。
如何根据子查询加载Flutter下拉按钮?
{
"companiesList":{
"companyBranch":[
{
"company":{
"bKd":"0001",
"bAd":"Comany_A",
"bIlAd":"England"
},
"branches":[
{
"bKd":"0001",
"sKd":"00001",
"sAd":"London",
"sIlKd":"006"
},
......
]
},
{
"company":{
"bKd":"0004",
"bAd":"company_B",
"bIlAd":"Germany"
},
"branches":[
{
"bKd":"0004",
"sKd":"90001",
"sAd":"Berlin",
"sIlKd":"999"
},
......
]
}
]
}
}
在initState中,我调用future来加载json,然后使用另一个future来加载项,如下所示。而且我使用 itemsComanyNames 加载下拉列表。
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
bool _autoValidate = false;
bool jsonLoad = false;
List<Map> _myCompanies;
String _myCompany;
// TODO: (1) - initState
@override
void initState() {
super.initState();
loadJson();
}
Future loadJson() async {
String jsonAccount = await rootBundle
.loadString("packages/niyazi_fixdropdown/company.json");
setState(() {
_myCompanies = List<Map>.from(jsonDecode(jsonAccount) as List);
jsonLoad = true;
});
}
@override
Widget build(BuildContext context) {
return jsonLoad != true
? new Scaffold(body: new Center(child: new CircularProgressIndicator()))
: new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new SafeArea(
top: false,
bottom: false,
child: new Form(
key: _formKey,
autovalidate: _autoValidate,
child: new ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: <Widget>[
new FormField<String>(
builder: (FormFieldState<String> state) {
return new DropdownButton(
value: _myCompany,
isDense: false,
items: _myCompanies((c) {
return new DropdownMenuItem(
value:
"${c["bAd"].toString()}",
child: new Text(
"${c["bAd"].toString()}",
maxLines: 2,
softWrap: true,
style: new TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.w500,
),
),
);
}).toList(),
hint: new Text(“Choose a Company…”,
maxLines: 1,
softWrap: true,
style: new TextStyle(
fontSize: 18.0,
color: Colors.red.shade700,
fontWeight: FontWeight.w500,
)),
onChanged: (String value) {
// ???????????
// ???????????
});
},
),
new Container(
padding:
const EdgeInsets.only(left: 40.0, top: 20.0),
child: new RaisedButton(
child: const Text('Submit'),
onPressed: null,
)),
],
))),
);
}
}
Update-1::我在_myCompanies.map((c)行上显示为 NoSuchMethodError 的错误:方法'map'在null上调用。
更新2:新错误: _InternalLinkedHashMap不是在_myCompanies = List.from(jsonDecode(jsonAccount)as List)上强制转换的类型“ List”的子类型;线