Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Question) how to rebuild every value in BuiltList? #250

Open
linxkaa opened this issue Aug 19, 2021 · 1 comment
Open

(Question) how to rebuild every value in BuiltList? #250

linxkaa opened this issue Aug 19, 2021 · 1 comment

Comments

@linxkaa
Copy link

linxkaa commented Aug 19, 2021

I'm new to built list, there's this thing I cant understand, so I have a model called AdressShipmentModel

In that model i have this value (I'm using freezed package)

@freezed
abstract class AdressShipmentModel with _$AdressShipmentModel {
  const factory AdressShipmentModel({
    required int addrId,
    required String label,
    required String receiverName,
    required String receiverPhone,
    required String address,
    required String isDefault,
    required int locationId,
    required String locationName,
    required String latlng,
    @Default(false) @JsonKey(ignore: true) bool isChoosenAddress,
  }) = _AdressShipmentModel;

  factory AdressShipmentModel.fromJson(Map<String, dynamic> json) => _$AdressShipmentModelFromJson(json);
}

The goal is to modify the model that choose by user is default by parameter isChoosenAddress

By this code I'm able to modify the choosen address to true if isDefault parameter is "1".

  initialData() {
    List<AdressShipmentModel> oldValue = value.toList();
    List<AdressShipmentModel> newValue = [];
    oldValue.forEach((e) {
      if (e.isDefault == "1") {
        newValue.add(e.copyWith(isChoosenAddress: true));
      } else {
        newValue.add(e.copyWith(isChoosenAddress: false));
      }
    });

    return ChoosenAddressEntities._(BuiltList<AdressShipmentModel>(newValue));
  }

But this was the old method of modifying list, and there's really no need for built_collection package if I do it like this.

When I use .rebuild(), it will not modify list as I wanted, both of the isChoosenAddress was set to false. (Not modified)
image

Is there's something I do wrong? Any kind of explanation will be so much appreciated! Thank you :)

This was my full code of logic:

import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import '/infrastructure/models/settings/address_shipment_model.dart';

class ChoosenAddressEntities extends Equatable {
  final BuiltList<AdressShipmentModel> value;
  const ChoosenAddressEntities._(this.value);
  factory ChoosenAddressEntities(List<AdressShipmentModel> listOfCart) =>
      ChoosenAddressEntities._(BuiltList<AdressShipmentModel>(listOfCart));

  updateChoosenAddress({
    required int id,
  }) {
    int index = _getAddressByIndex(id);
    BuiltList<AdressShipmentModel> newValue = value.rebuild((e) {
      e[index] = e[index].copyWith(isChoosenAddress: true);

      return _replaceRange(index: index, newAddressItem: e[index]);
    })
      ..forEach((e) {
        if (e.addrId != index) {
          e.copyWith(isChoosenAddress: false);
        }
      });
    return ChoosenAddressEntities._(newValue);
  }

  initialData() {
    List<AdressShipmentModel> oldValue = value.toList();
    List<AdressShipmentModel> newValue = [];
    oldValue.forEach((e) {
      if (e.isDefault == "1") {
        newValue.add(e.copyWith(isChoosenAddress: true));
      } else {
        newValue.add(e.copyWith(isChoosenAddress: false));
      }
    });

    return ChoosenAddressEntities._(BuiltList<AdressShipmentModel>(newValue));
  }

  int _getAddressByIndex(int id) {
    return value.indexOf(value.firstWhere((e) => e.addrId == id));
  }

  BuiltList<AdressShipmentModel> _replaceRange({
    required int index,
    AdressShipmentModel? newAddressItem,
  }) {
    return value.rebuild(
      (a) => a
        ..replaceRange(
          index,
          index + 1,
          newAddressItem != null ? [newAddressItem] : [],
        ),
    );
  }

  @override
  // TODO: implement props
  List<Object?> get props => [value];
}
@dave26199
Copy link

dave26199 commented Aug 19, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants