如何将本地json加载到List< Map>多变的? [英] How do I load my local json into List<Map> variable?
问题描述
如何将本地json加载到List<地图>变量?
How do I load my local json into List< Map > variable?
这是我的本地json.
[
{"id": 00", "name": "TRL"},
{"id": 01", "name": "USD"},
{"id": 02", "name": "GBP"},
{"id": 03", "name": "EUR"},
]
这是可行的:
List<Map> _myCurrency = [
{"id": 00", "name": "TRL"},
{"id": 01", "name": "USD"},
{"id": 02", "name": "GBP"},
{"id": 03", "name": "EUR"},
];
我的问题是我将货币数据移动到currency.json文件中.我可以加载json,但不能分配给List<地图>变量.有什么帮助吗?
My problem is I move my currency data into currency.json file. I can load the json but I cannot assign to List< Map > variable. Any help please?
更新:
String jsonTCBanks =
await rootBundle.loadString("packages/capi/currency.json");
List<Map> _myCurrency = json.decode(jsonTCBanks);
我收到错误消息;
type 'List<dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>'
如果我使用Map _myCurrency,则json.decode可以工作,但是我松开了键,值属性.
If I use Map _myCurrency the json.decode works, but I loose the key, value properties.
UPDATE-2: 我一直收到以下错误消息:
UPDATE-2: I am keep getting error as:
I/flutter (16273): The following assertion was thrown building MyHomePage(dirty, state: _MyHomePageState#44865):
I/flutter (16273): type 'MappedListIterable<Map<dynamic, dynamic>, DropdownMenuItem<dynamic>>' is not a subtype of type
I/flutter (16273): 'List<DropdownMenuItem<String>>'
class _MyHomePageState extends State<MyHomePage> {
String _mySelectedCurrency;
List<Map> _myCurrencies;
@override
void initState() {
// TODO: implement initState
super.initState();
_loadLocalJsonData();
}
Future _loadLocalJsonData() async {
String jsonCurrency = await rootBundle
.loadString("packages/capi/currency.json");
setState(() {
_myCurrencies = List<Map>.from(jsonDecode(jsonCurrency) as List);
print("*******_myCurrencies: $_myCurrencies");// This part works correctly
});
}
@override
Widget build(BuildContext context) {
return _myCurrencies == null ? _buildWait(context) : _buildRun(context);
}
// TODO: BUILD RUN
Widget _buildRun(BuildContext context) {
final _itemsName = _myCurrencies.map((c) {
return new DropdownMenuItem<String>(
value: c["id"].toString(),
child: new Text(c["name"].toString()),
);
}).toList();
return new Scaffold(
key: _scaffoldKey,
body: new SafeArea(
top: false,
bottom: false,
child: new Form(
key: _formKey,
child: new ListView(
padding: const EdgeInsets.symmetric(
horizontal: 16.0, vertical: 32.0),
children: <Widget>[
//TODO: CURRENCY ###########################################
new FormField<String>(
builder: (FormFieldState<String> state) {
return InputDecorator(
decoration: InputDecoration(
labelText: 'CHOOSE CURRENCY',
labelStyle: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.green.shade700),
errorText: state.hasError ? state.errorText : null,
),
isEmpty: _mySelectedCurrency == '',
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
style: TextStyle(
fontSize: 14.0,
color: Colors.black,
fontWeight: FontWeight.w500,
),
value: _mySelectedCurrency,
isDense: true,
onChanged: (String newValue) {
setState(() {
_mySelectedCurrency = newValue;
state.didChange(newValue);
});
},
items: _itemsName,
),
),
);
},
validator: (val) {
return val != '' ? null : 'Choose Currency...';
},
),
],
))));
}
// TODO: BUILD WAIT
Widget _buildWait(BuildContext context) {
return new Scaffold(
body: new Center(child: CircularProgressIndicator()),
);
}
}
推荐答案
您需要将其从字符串解码为数据结构,并通过创建具有所需类型的新列表来调整类型,并在其中传递从<返回的列表c0>:
You need to decode it from a string to a data structure and adjust the type by creating a new list with the desired type where you pass the list returned from jsonDecode
:
import 'dart:convert';
...
List<Map> _myCurrency = List<Map>.from(jsonDecode(json) as List);
final currencyItems = _myCurrency.map(
(c) => DropdownMenuItem<String>(c['id'], child: Text(c['name'])).toList();
DropdownButton(items: currencyItems);
这篇关于如何将本地json加载到List< Map>多变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!