Home > Net >  Using Dart in Flutter, how do I add to a list of maps that is inside of a list of maps?
Using Dart in Flutter, how do I add to a list of maps that is inside of a list of maps?

Time:09-22

I'm somewhere between beginner and intermediate level using Dart/Flutter combination.

I'm building a Marine logbook whereby I need to be able to log a duty that is made up a a variable number of activities. See the code below. In this example I have two duties that each comprise 1 activity.

    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?:

        {
          '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. Just wondering as well how I might save an activity in firestore? I'm happy with saving a duty in firestore but haven't taken the leap into saving lists in lists yet.

Hope that all makes sense.

Thanks in advance and apologies if this has been covered previously. I did check prior to posting but didn't see anything of help.

CodePudding user response:

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

[{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}]}]
  • Related