skip to Main Content

I’m trying to check if an id from one class already exists in the other for example:

Class pin{
String id;
int amount;
}

Class pinCol{
String id;
String name;
}

this is just an example to show there are two classes that both have an id value but the other values differ.

now I want to see if the new ids of the first are still the same as the IDs in the second without deleting all the values and just replacing them.
for example, I want to see if the id’s in pins is still the same as the id’s in pinCol, any new ids I want to add and any ids that exist in pinCol and not in pins I want to remove from pinCol

2

Answers


  1. Assuming pins is a list of pin objects and pinCols is a list of pinCol objects, you could do this to remove all pinCols that don’t have an id that it’s in the list of pins:

    pinCols.removeWhere((pinCol) => !pins.any((pin) => pin.id == pinCol.id));
    
    Login or Signup to reply.
  2. What are you trying to achieve?

    • You have to use Extend, Class "pin" to "pinCol".

    If you have to match the value then the key name should be different otherwise it’s overridden.

    class pin {
      final String id = "1";
      final int amount = 2;
    }
    
    class pinCol extends pin {
      String id1 = '1';
      String name1 = '2';
      pinCol({id, amount});
    }
    
    void main() {
      var pincol = pinCol();
      if(pincol.id == pincol.id1){
        print("Value Match");
      }
    }
    

    If Both class key names are the same then the value will be overridden. Like below example

    Example

    class pin {
      final String id = "1";
      final int amount = 2;
    }
    
    class pinCol extends pin {
      String id = '2';
      String name1 = 'Nitin';
      pinCol({id, amount});
    }
    
    void main() {
      var pincol = pinCol();
       print(pincol.id); // Result => 2
    }
    

    Maybe it’ll help for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search