简体   繁体   English

在 Flutter 中使用 Dart,如何添加到地图列表中的地图列表?

[英]Using Dart in Flutter, how do I add to a list of maps that is inside of a list of maps?

I'm somewhere between beginner and intermediate level using Dart/Flutter combination.我使用 Dart/Flutter 组合介于初级和中级之间。

I'm building a Marine logbook whereby I need to be able to log a duty that is made up aa variable number of activities.我正在建立一个航海日志,我需要能够记录由可变数量的活动组成的职责。 See the code below.请参阅下面的代码。 In this example I have two duties that each comprise 1 activity.在这个例子中,我有两个职责,每个职责包含 1 个活动。

    List<Map<String, dynamic>> duties = [
        {
          'date': '12122021',
          'dutyStart': '00:00',
          'dutyFinish': '12:00',
          'skipper': 'Mark',
          'crew': 'My Crew',
          'assets': 'G1',
          'activity': [
            {
              'activityType': 'Training',
              'trainingActivityDetail': 'Navigation',
              'wapolJobNumber': null,
              'rapNumber': null,
              'activityStart': '02:00',
              'activityFinish': '03:00',
              'startFuel': 100,
              'endFuel': 300,
              'fuelUsed': 200,
            }
          ],
        },
        {
          'date': '01092021',
          'dutyStart': '18:00',
          'dutyFinish': '23:00',
          'skipper': 'Neil',
          'crew': 'My Crew',
          'assets': 'G2',
          'activity': [
            {
              'activityType': 'WAMSAR',
              'trainingActivityDetail': null,
              'wapolJobNumber': '123',
              'rapNumber': '456',
              'activityStart': '19:00',
              'activityFinish': '20:00',
              'startFuel': 1000,
              'endFuel': 3000,
              'fuelUsed': 2000,
            }
          ],
        }
      ];

I've worked out how to add a duty with the following code:我已经研究出如何使用以下代码添加职责:

//add a duty
  duties.add(
    {
      'date': '111111',
      'dutyStart': '11:11',
      'dutyFinish': '22:22',
      'skipper': 'Tom',
      'crew': 'My Crew',
      'assets': 'Ranger',
      'activity': [
        {
          'activityType': 'WAMSAR',
          'trainingActivityDetail': null,
          'wapolJobNumber': '000',
          'rapNumber': '999',
          'activityStart': '19:00',
          'activityFinish': '20:00',
          'startFuel': 1000,
          'endFuel': 3000,
          'fuelUsed': 2000,
        }
      ],
    },
  );

But I can't work out how to add an activity to a duty.但我无法弄清楚如何将活动添加到职责中。

For example, how would I add the activity below to the last duty so that the last duty now has 2 activities instead of 1?:例如,我如何将下面的活动添加到最后一个职责中,以便最后一个职责现在有 2 个活动而不是 1 个?:

        {
          'activityType': 'XXX',
          'trainingActivityDetail': null,
          'wapolJobNumber': '000',
          'rapNumber': '999',
          'activityStart': '19:00',
          'activityFinish': '20:00',
          'startFuel': 1000,
          'endFuel': 3000,
          'fuelUsed': 2000,
        }

For now I'm just using the data in a listview builder that isn't being saved anywhere.现在我只是在一个没有保存在任何地方的列表视图构建器中使用数据。 It will end up being saved in firestore cloud.它最终将保存在 firestore 云中。 Just wondering as well how I might save an activity in firestore?只是想知道如何在 Firestore 中保存活动? I'm happy with saving a duty in firestore but haven't taken the leap into saving lists in lists yet.我很高兴在 firestore 中保存一项职责,但还没有跳到在列表中保存列表。

Apologies if this has been covered previously.抱歉,如果之前已经涵盖了这一点。 I did check prior to posting but didn't see anything of help.我在发布之前确实进行了检查,但没有看到任何帮助。

you can try this.你可以试试这个。 Here we insert the activity element in the last duty as follows:这里我们在最后一个职责中插入活动元素,如下所示:

  duties.last["activity"].add(
        {
          'activityType': 'WAMSAR',
          'trainingActivityDetail': null,
          'wapolJobNumber': '000',
          'rapNumber': '999',
          'activityStart': '19:00',
          'activityFinish': '20:00',
          'startFuel': 1000,
          'endFuel': 3000,
          'fuelUsed': 200023,
        }
     );
    
    print(duties);

Output Output

[{date: 12122021, dutyStart: 00:00, dutyFinish: 12:00, skipper: Mark, crew: My Crew, assets: G1, activity: [{activityType: Training, trainingActivityDetail: Navigation, wapolJobNumber: null, rapNumber: null, activityStart: 02:00, activityFinish: 03:00, startFuel: 100, endFuel: 300, fuelUsed: 200}]}, {date: 01092021, dutyStart: 18:00, dutyFinish: 23:00, skipper: Neil, crew: My Crew, assets: G2, activity: [{activityType: WAMSAR, trainingActivityDetail: null, wapolJobNumber: 123, rapNumber: 456, activityStart: 19:00, activityFinish: 20:00, startFuel: 1000, endFuel: 3000, fuelUsed: 2000}, {activityType: WAMSAR, trainingActivityDetail: null, wapolJobNumber: 000, rapNumber: 999, activityStart: 19:00, activityFinish: 20:00, startFuel: 1000, endFuel: 3000, fuelUsed: 200023}]}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM