一、forEach
import 'package:flutter/material.dart';
import 'data.dart';
void main() {
runApp(MyApp());
}
// 自定义组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("flutter"),
),
body: HomeContent()),
);
}
}
class HomeContent extends StatelessWidget {
List<Widget> getData() {
List<Widget> arr = new List();
data.forEach((item) {
arr.add(ListTile(
title: Text(item["title"]),
subtitle: Text(item["subtitle"]),
));
});
return arr;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return ListView(children: this.getData());
}
}
二、map
List<Widget> _getData(){
var templist = ListData.map((value){
return ListTile(
leading: Image.network(value["ImgUrl"]),
title: Text(value["title"]),
subtitle: Text(value["title"]),
);
});
return templist.toList();
}