Home > Blockchain >  Order json list for personal field
Order json list for personal field

Time:01-11

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

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...

CodePudding user response:

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.

  • Related