List 颤振如何从其他文件加载列表?
我想将一个列表从一个文件加载到一个小部件中,我有几个列表,应该加载一个带有id的特定列表 这是我的代码:List 颤振如何从其他文件加载列表?,list,flutter,List,Flutter,我想将一个列表从一个文件加载到一个小部件中,我有几个列表,应该加载一个带有id的特定列表 这是我的代码: main class Cars extends StatefulWidget { @override _carsState createState() => _carsState(); } class _carsState extends State<cars> { List clist = [ {'id': 'l0', 'name': 'BMW'
main
class Cars extends StatefulWidget {
@override
_carsState createState() => _carsState();
}
class _carsState extends State<cars> {
List clist = [
{'id': 'l0', 'name': 'BMW'},
{'id': 'l1', 'name': 'Audi'},
];
@override
Widget build(BuildContext context) {
return Column(
children: clist.map((info) {
return Container(
margin: EdgeInsets.all(5),
child: SizedBox(
width: double.infinity,
child: RaisedButton(
padding: EdgeInsets.all(20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
),
color: Colors.blue,
child: Text(
info['name'],
style: TextStyle(color: Colors.white),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Cars(name: info['name'], id: info['id'],)),
);
},
),
),
);
}).toList(),
);
}
}
list.dart
class Cars extends StatelessWidget {
final String name;
var id;
Cars({Key key, this.name, this.id}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(name),
backgroundColor: Colors.green,
centerTitle: true,
),
body: Column(
children: LISTFROMOTHERFILE.map((info) {
return Container(
margin: EdgeInsets.all(5),
child: SizedBox(
width: double.infinity,
child: Text(info['name'])
),
);
}).toList(),
));
}
}
List l0 = [
{'name': 'Example', 'PS': '500'},
{'name': 'Example2', 'PS': '300'},
];
List l1 = [
{'name': 'Example', 'PS': '300'},
];
现在,例如,如果ID 1是从main传输的,我希望加载列表l0
我如何实现这一点,或者有更好的方法吗?最好将POJO类列为您的列表
class ExampleList{
final String id;
final String name ;
final String PS;
/// implement [fromJson] and [toJson] method if needed
}
现在,无论何时需要它,只要传递整个对象,而不是传递字符串
你可以这样使用它
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Cars(ExampleList:ExampleList)),
);
为此,我将研究JSON。我想你不明白我想说什么!很抱歉现在意识到请参考这个不,那也是不同的,我有不同的列表,并希望加载一个列表,这取决于从主服务器传输的id,列表依次在list.dart中的一个额外文件中