I have a question how to keep all data in sublist created from main list in Flutter? I created simple code to explain what i am looking for. I managed to save one value - swimmer.nameModel but i need all of them. Is any good tutorial to learn this subject properly? Thanks in advance. Here is my code:
import 'package:flutter/material.dart';
class ModelUser {
final String nameModel;
final String surnameModel;
final String? imageModel;
final String? mobileModel;
final String? emailModel;
final String? addressModel;
final List<String?> joinedGroupsModel;
const ModelUser({
Key? key,
required this.nameModel,
required this.surnameModel,
this.imageModel,
this.mobileModel,
this.emailModel,
this.addressModel,
this.joinedGroupsModel = const [],
});
}
List<ModelUser> allUsers = [
const ModelUser(
nameModel: 'John',
surnameModel: 'Newman',
joinedGroupsModel: ['Swimmers', 'Runners', 'Footballers']),
const ModelUser(nameModel: 'Barry', surnameModel: 'Newday'),
const ModelUser(
nameModel: 'Mike',
surnameModel: 'Smith',
joinedGroupsModel: ['Swimmers', 'Runners', 'Footballers']),
const ModelUser(
nameModel: 'Harry',
surnameModel: 'Portland',
joinedGroupsModel: ['Footballers']),
const ModelUser(
nameModel: 'Cathy',
surnameModel: 'Portman',
joinedGroupsModel: ['Swimmers', 'Runners', 'Footballers']),
const ModelUser(
nameModel: 'Jessie',
surnameModel: 'Olert',
joinedGroupsModel: ['Swimmers']),
];
class PageWhere extends StatelessWidget {
static const String id = 'PageWhere';
const PageWhere({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final swimmers =
allUsers.where((user) => user.joinedGroupsModel.contains('Swimmers'));
final List<String> listOfSwimmers = swimmers.map((swimmer) {
return swimmer.nameModel;
}).toList();
return Column(
children: [
Text('Swimmers'),
...listOfSwimmers
.map((value) => Text(value, style: TextStyle(fontSize: 20)))
.toList(),
],
);
}
}
So instead of List-String listOfSwimmers how can I create List-ModelUser listOfSwimmers with all values provided in allUsers list?