数据将是这样的
[{ID: 1, Code: 01, Description: REGION I (ILOCOS REGION), PSGCCode: 010000000}, {ID: 2, Code: 02, Description: REGION II (CAGAYAN VALLEY), PSGCCode: 020000000},
Run Code Online (Sandbox Code Playgroud)
我只想使用描述作为 DropDownButton 中的文本
编辑****
类看起来像这样吗?
class Region {
final int regionid;
final String regionDescription;
Region ({
this.regionid,
this.regionDescription
});
factory Region.fromJson(Map<String, dynamic> json) {
return new Region(
regionid: json['id'],
regionDescription: json['description']
);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑**尝试使用或执行上面的类并将其分配给列表
List<Region> _region = [];
Run Code Online (Sandbox Code Playgroud)
并将其用于我的 DropDownItems
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
hint: new Text("Select Region"),
value: selectedRegion,
isDense: true,
onChanged: (String newValue) {
setState(() {
selectedRegion = newValue;
});
print(selectedRegion);
},
items: _region.map((Region map) {
return new DropdownMenuItem<String>(
value: map.regionDescription,
child: new Text(map.regionDescription,
style: new TextStyle(color: Colors.black)),
);
}).toList(),
Run Code Online (Sandbox Code Playgroud)
每次尝试点击 DropdownButton 时都会捕获异常
I/flutter (11272): ??? EXCEPTION CAUGHT BY RENDERING LIBRARY ??????????????????????????????????????????????????????????
I/flutter (11272): The following ArgumentError was thrown during paint():
I/flutter (11272): Invalid argument(s): 0.0
I/flutter (11272): When the exception was thrown, this was the stack:
I/flutter (11272): #0 double.clamp (dart:core/runtime/libdouble.dart:143:7)
I/flutter (11272): #1 _DropdownMenuPainter.paint (package:flutter/src/material/dropdown.dart:57:33)
I/flutter (11272): #2 RenderCustomPaint._paintWithPainter (package:flutter/src/rendering/custom_paint.dart:520:13)
I/flutter (11272): #3 RenderCustomPaint.paint (package:flutter/src/rendering/custom_paint.dart:558:7)
I/flutter (11272): #4 RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2018:7)
I/flutter (11272): #5 PaintingContext.paintChild (package:flutter/src/rendering/object.dart:130:13)
Run Code Online (Sandbox Code Playgroud)
如果我们假设您的源数据是正确格式化的 JSON,那么items您的属性DropdownButton将类似于:
import 'dart:convert';
var data = '[{"ID":"1", ...';
var json = JsonDecoder().convert(data);
// …
items: json.map<String>((item) => DropdownMenuItem<String>(
value: item['Description'],
child: Text(item['Description'])),
Run Code Online (Sandbox Code Playgroud)
根据您的使用情况以及其他使用数据,但它可能有助于有一个代表你的数据项的类,那么你可以使用map()上json(从上面的例子)来创建数据结构的列表,然后从地图这些值到items的DropdownMenuItem。
这是一个完整的工作示例:
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final String data =
'[{"ID": 1, "Code": "01", "Description": "REGION I (ILOCOS REGION)", "PSGCCode": "010000000"}, {"ID": 2, "Code": "02", "Description": "REGION II (CAGAYAN VALLEY)", "PSGCCode": "020000000"}]';
List<Region> _region = [];
String selectedRegion;
@override
Widget build(BuildContext context) {
final json = JsonDecoder().convert(data);
_region = (json).map<Region>((item) => Region.fromJson(item)).toList();
selectedRegion = _region[0].regionDescription;
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButtonHideUnderline(
child: new DropdownButton<String>(
hint: new Text("Select Region"),
value: selectedRegion,
isDense: true,
onChanged: (String newValue) {
setState(() {
selectedRegion = newValue;
});
print(selectedRegion);
},
items: _region.map((Region map) {
return new DropdownMenuItem<String>(
value: map.regionDescription,
child: new Text(map.regionDescription,
style: new TextStyle(color: Colors.black)),
);
}).toList(),
),
),
],
),
),
);
}
}
class Region {
final int regionid;
final String regionDescription;
Region({this.regionid, this.regionDescription});
factory Region.fromJson(Map<String, dynamic> json) {
return new Region(
regionid: json['ID'], regionDescription: json['Description']);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18949 次 |
| 最近记录: |