Get data from a list inside a list

32 views
Skip to first unread message

Marc Andrew

unread,
Jun 16, 2018, 5:01:13 PM6/16/18
to Flutter Dev
Hi,

I have a ListView with data from a list. The problem I have is to get the data from a list which is inside a list.

My list looks like this below

class Workout {
const Workout({
this.id,
this.title,
this.exercises
});

final int id;
final String title;
final List<ExerciseDetail> exercises;
}

class ExerciseDetail {
const ExerciseDetail({
this.title,
});

final String title;
}

List<Workout> workouts = [
const Workout(
id: 1,
title: "Workout 1",
exercises: const <ExerciseDetail>[
const ExerciseDetail(
title: "Exercise 1",
),
const ExerciseDetail(
title: "Exercise 2",
),
]
),
const Workout(
id: 2,
title: "Workout 2",
exercises: const <ExerciseDetail>[
const ExerciseDetail(
title: "Exercise 1",
),
const ExerciseDetail(
title: "Exercise 2",
),
const ExerciseDetail(
title: "Exercise 3",
),
]
),
];

And the code below is to build the ListView

import 'package:flutter/material.dart';

// Model
import '../model/workouts.dart';

class WorkoutsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: workouts.length,
itemBuilder: (context, index) => new WorkoutItem(workouts[index]),
);
}
}

class WorkoutItem extends StatelessWidget {
final Workout workout;

WorkoutItem(this.workout);

@override
Widget build(BuildContext context) {
return new ListTile(
title: new Text(workout.title),
subtitle: new Row(
children: <Widget>[
// Get title List<Workout> > exercises
// new Text() from exercises
],
),
trailing: new Icon(
Icons.arrow_forward_ios,
size: 16.0,
),
);
}
}


Can anyone help with this please?

Thank you

Danny Tuppeny

unread,
Jun 17, 2018, 6:14:06 PM6/17/18
to Flutter Dev
Assuming you want to create a Widget for each item in the list, I think calling .map() will do what you want?

children: workout.excercises.map((ex) => new Text(ex.title)),

Jonah Williams

unread,
Jun 17, 2018, 6:40:52 PM6/17/18
to Danny Tuppeny, Flutter Dev
don't forget `toList()`!

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Danny Tuppeny

unread,
Jun 18, 2018, 1:33:57 AM6/18/18
to Jonah Williams, Flutter Dev
On Sun, 17 Jun 2018 at 23:40 Jonah Williams <jonahw...@google.com> wrote:
don't forget `toList()`!

I think I need this tattooed in my arm! I do hope one day it might become an error!

Marc Andrew

unread,
Jun 18, 2018, 6:07:30 PM6/18/18
to Flutter Dev
Thank you guys for your help!
Reply all
Reply to author
Forward
0 new messages