Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order json list for personal field

I have a list model from json that I showing in ModalBottomSheet, but I want to show at the first element the current object selected.

I save the idSalon in the flutterSecureStorage()

listsSalon.sort((a, b) {
    return a.id.compareTo(idSalon!);
  });
Expanded(
              child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: listsSalon.length,
                  itemBuilder: ((BuildContext context, int index) {
                    return SalonWidget(
                        salons: listsSalon,
                        translations: translations,
                        idSalonSelected: idSalon,
                        position: index);
                  })),
            ),

This is my model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class SalonsModel {
  final String id;
  final String name;
  final String address;
  final String countryInfo;
  final String phone;
  final String imgUrl;
  final String latitude;
  final String longitude;
  final String salonCode;

  const SalonsModel(
      {required this.id,
      required this.name,
      required this.address,
      required this.countryInfo,
      required this.phone,
      required this.imgUrl,
      required this.latitude,
      required this.longitude,
      required this.salonCode});

  factory SalonsModel.fromJson(Map<String, dynamic> parsedJson) {
    return SalonsModel(
      id: parsedJson['id'],
      name: parsedJson['name'],
      address: parsedJson['address'],
      countryInfo: parsedJson['country_info'],
      phone: parsedJson['phone'],
      imgUrl: parsedJson['imgUrl'],
      latitude: parsedJson['latitude'],
      longitude: parsedJson['longitude'],
      salonCode: parsedJson['salon_code'],
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "address": address,
        "country_info": countryInfo,
        "phone": phone,
        "imgUrl": imgUrl,
        "latitude": latitude,
        "longitude": longitude,
        "salon_code": salonCode,
      };
}

I don’t think it’s because it’s in a modal bottom sheet, but not working…

>Solution :

You can try this:

SalonsModel selectedSalon = listsSalon.firstWhere((element) => element. id == idSalon!);
listsSalon.remove(selectedSalon);
listsSalon.insert(0, selectedSalon);

first we remove that salon then insert it at 0 index.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading