一、forEach

  1. import 'package:flutter/material.dart';
  2. import 'data.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. // 自定义组件
  7. class MyApp extends StatelessWidget {
  8. @override
  9. Widget build(BuildContext context) {
  10. // TODO: implement build
  11. return MaterialApp(
  12. home: Scaffold(
  13. appBar: AppBar(
  14. title: Text("flutter"),
  15. ),
  16. body: HomeContent()),
  17. );
  18. }
  19. }
  20. class HomeContent extends StatelessWidget {
  21. List<Widget> getData() {
  22. List<Widget> arr = new List();
  23. data.forEach((item) {
  24. arr.add(ListTile(
  25. title: Text(item["title"]),
  26. subtitle: Text(item["subtitle"]),
  27. ));
  28. });
  29. return arr;
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. // TODO: implement build
  34. return ListView(children: this.getData());
  35. }
  36. }

二、map

  1. List<Widget> _getData(){
  2. var templist = ListData.map((value){
  3. return ListTile(
  4. leading: Image.network(value["ImgUrl"]),
  5. title: Text(value["title"]),
  6. subtitle: Text(value["title"]),
  7. );
  8. });
  9. return templist.toList();
  10. }