Home > Blockchain >  How to compare my two projectModels objects one is from a local value and the other from a future? f
How to compare my two projectModels objects one is from a local value and the other from a future? f

Time:05-28

I'm trying to compare two objects and see if they have the same values, one of the object comes from an future api call

ProjectModel response = await ProjectServices.fetchSingleProjectStream(
        projectId: projectId,
      );

and the other one from a local value which I fill with another api call whom I call onInit()

final _projectModel = ProjectModel(
    guid: '',
    projectColor: '',
    title: '',
    tags: [],
    tasks: [],
    teamMembers: [],
  ).obs;
  fetchSingleProject() async {
    try {
      if (_projectModel.value.tasks!.isEmpty) {
        _isLoading(true);
      }
      ProjectModel response = await ProjectServices.fetchSingleProjectStream(
        projectId: projectId,
      );
      print('from local: ${_projectModel.value.tasks![0].description}');
      print('from api: ${response.tasks![0].description}');
      // var shouldReload = projectModel.tasks!
      //     .any((element) => _projectModel.value.tasks!.contains(element));
      // bool shouldReload = response.tasks == _projectModel.value.tasks;
      const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
      bool shouldReload = dc.equals(
        response.tags!.toList(),
        _tagsList.toList(),
      );
      print('should the stream reload: $shouldReload');
      if (shouldReload) {
        _projectModel.value = response;
        _tagsList.assignAll(response.tags!);
      }
    } catch (e) {
      throw Get.snackbar('Opss', 'An error occourred');
    } finally {
      _isLoading(false);
    }
  }

  fetchInitialProject() async {
    try {
      _isLoading(true);
      ProjectModel response = await ProjectServices.fetchSingleProjectStream(
        projectId: projectId,
      );
      _projectModel.value = response;
      _tagsList.assignAll(response.tags!);
    } catch (e) {
      throw Get.snackbar('Error', '$e');
    } finally {
      _isLoading(false);
    }
  }

  @override
  Future<void> onInit() async {
    if (_projectModel.value.tasks!.isEmpty) await fetchInitialProject();
    super.onInit();
  }

  @override
  void onReady() {
    timer = Timer.periodic(const Duration(seconds: 3), (timer) async {
      await fetchSingleProject();
    });
    super.onReady();
  }

And when I print the values of each of the object they come out the same but the result comes out false why is that and how do I fix it?

      print('from local: ${_projectModel.value.tasks![0].description}');
      print('from api: ${response.tasks![0].description}');
---
flutter: from local: This is an autogenerated #tbd, @ShemsedinRaketa
flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa
---

Method 1 of trying to compare the tasksList:

bool shouldReload = response.tasks == _projectModel.value.tasks;

// returns false

Method 2 of trying to compare the tasksList:

// importing form the collection package
      const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
      bool shouldReload = dc.equals(
        response.tags!.toList(),
        _tagsList.toList(),
      );
// returns false as well

Method 3 of tryingto compare the tasksList:

      var shouldReload = projectModel.tasks!
          .any((element) => _projectModel.value.tasks!.contains(element));
// even if all the values are not the same it still returns true when 
// only one of the values matches
// flutter: from local: This is an autogenerated #tbd,
// @ShemsedinRaketa
// flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa poakdhmnbs
// flutter: should the stream reload: true

What I want to achieve with this is a stream which I want to be able to control when to load the new data which comes from the api and when not, which is when the data that comes from the api is the same as those in the local then don't update the UI.

CodePudding user response:

bool areEqual(List<Person> a, List<Person> b){
  for(int i = 0;i<a.length;i  ){
    if(a[i].id != b[i].id){
      return false;
    }
  }
  return true;
}

void main(){
List<Person> a = [Person(),Person()];
List<Person> b = [Person(),Person()];
}

This is how your function could look like, but change it to your Objects not Person and value is not id, but description for you

  • Related