Flutter:映射两个列表

时间:2018-09-24 18:42:27

标签: dictionary flutter

在这里,我仅映射一个列表,但是这种情况是我想将两个列表映射到一起,以便出现日期,之后的照片如此处图片所示。因此,我想知道如何制作list1和date的映射,这是我拥有的两个列表。他们只是我可以在脚手架上使用的一个清单? enter image description here

List<Meal> _list1=[Meal(dis:'it is a chicken sandwichh',name: 'chekin filla'),
Meal(dis:'it is a chicken sandwichh',name: 'chekin filla'),
Meal(dis:'it is a chicken sandwichh',name: 'chekin filla'),
Meal(dis:'it is a chicken sandwichh',name: 'chekin filla'),
Meal(dis:'it is a chicken sandwichh',name: 'chekin filla')];
List<Date> dates = [Date(dat:'20th of september 2018'),Date(dat:'21th of september 2018'),Date(dat:'22th of september 2018'),
Date(dat:'23th of september 2018')
,Date(dat: '24th of september 2018')];
Map<Object,Object> list2= {dates,_list1};
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('listviewww'),
      ),
      body:
        ListView( children:_list2
        .map((element){
          return Column(
            children: <Widget>[
          Container(
              child: Text('My name is ayaaaaaaaaaaa '),
          ),
            Container(
            margin: EdgeInsets.all(9.0),
           constraints: new BoxConstraints.expand(height: 300.0),
           decoration: new BoxDecoration(
           border: new Border.all(color: Colors.blueAccent),
           borderRadius: BorderRadius.horizontal( left:Radius.circular(30.0) ,right:Radius.circular(30.0) ),
           image: new DecorationImage(image: new AssetImage('assets/food.jpg'),fit: BoxFit.cover,),),
           padding: EdgeInsets.only(bottom: 10.0),
      child:Center(
        child:Text('HELLfffffffffffO')
      )
      )
            ], 

      );  
        }).toList()

     ) 
     );

3 个答案:

答案 0 :(得分:1)

您不必映射两个列表即可创建import json import base64 with open('path_to_img.jpg', 'rb') as f: img_bytes = base64.b64encode(f.read()) json_data = {'image_bytes': {'b64': img_bytes.decode('ascii')}} with open('path_to_json_file.json', 'w+') as f: json.dump(json_data, f) 。只需使用ListView.builder()

ListView

答案 1 :(得分:0)

从我收集的内容来看,您有两个列表,每个列表具有相同的数字元素。第一个列表描述项目(名称,显示等),第二个列表描述日期。

您正尝试在列表中显示此信息,但由于您有两个信息列表而无法显示。

最简单的答案是将两个列表合并为一个列表,即将商品说明和日期结合在一起。您可以创建一个具有日期和描述作为字段的简单类。

从理论上讲,您可以使用Tuple库并由两个信息组成一个元组,但这似乎没有必要,而且使数据对象更简洁。

如果我对您的问题有误解,请随时告诉我。我可能看到的另一种可能性是,您正在尝试创建一个以日期为标题的列表,每个日期下都有一个或多个项目。

答案 2 :(得分:0)

我猜你想做类似的事情。选中下面的复选框,您可以run this code in Dartpad(您需要复制并粘贴)。

class Meal {
  Meal({this.dis, this.name});
  final String dis;
  final String name;
}

class Date {
  Date({this.dat});
  final String dat;
}

void main() {

  List<Meal> _list1 = [
    Meal(dis:'it is a chicken sandwichh', name: 'chekin filla'),
    Meal(dis:'it is a chicken sandwichh', name: 'chekin filla'),
    Meal(dis:'it is a chicken sandwichh', name: 'chekin filla'),
    Meal(dis:'it is a chicken sandwichh', name: 'chekin filla'),
    Meal(dis:'it is a chicken sandwichh', name: 'chekin filla')
  ];

  List<Date> dates = [
    Date(dat:'20th of september 2018'),
    Date(dat:'21th of september 2018'),
    Date(dat:'22th of september 2018'),
    Date(dat:'23th of september 2018'),
    Date(dat:'24th of september 2018')
  ];

  // this is where the mapping will be placed
  Map<Date, Meal> mealsEachDay = new Map();

  // and here the relationship between the dates and the meals is done
  for(var i = 0; i < dates.length; i++)
    mealsEachDay[dates[i]] = _list1[i];

  List<String> yourObjects = mealsEachDay.keys.map((date) {
    return "The date is '${date.dat}' and the meal is '${mealsEachDay[date].dis}'";
  }).toList();

  yourObjects.forEach((s) {
    print(s);    
  });
}

但是,这还不值钱。您必须更好地了解对象,列表和地图之间的关系。

检查下面的另一个示例,并在dartpad中运行它。每个约会都有饭,吃饭更容易。另一个改进是不使用日期作为文本。

class Meal {
  Meal({this.dis, this.name});
  final String dis;
  final String name;
}

class MealDate {
  MealDate({this.date, this.meals});
  final DateTime date;
  final List<Meal> meals;
}

void main() {

  List<MealDate> mealsForEachDate = [
    MealDate(
      date: DateTime(2018, 9, 20),
      meals: [
        Meal(name: 'eggs', dis: 'scrambed eggs'),
        Meal(name: 'chiken filet', dis: 'chicken sandwich'),
      ]
    ),
    MealDate(
      date: DateTime(2018, 9, 21),
      meals: [
        Meal(name: 'bread', dis: 'bread and water'),
      ]
    ),
    MealDate(
      date: DateTime(2018, 9, 22),
      meals: [
        Meal(name: 'rice', dis: 'rice and beans'),
      ]
    ),
  ];

  mealsForEachDate.forEach((date) {
    print("[Date ${date.date}]");
    print("  Number of meals ${date.meals.length}");
    var n = 0;
    date.meals.forEach((meal) {
      print("    Meal ${++n}: ${meal.dis}");
    });
    print("\n");
  });
}

祝您好运,永远不要停止编码!