Home > Back-end >  How to check if list contains a String, but String value is from other dart file
How to check if list contains a String, but String value is from other dart file

Time:04-15

As you can see I have a list:

  List avatarList = [
    AssetsResources.VIP1,
    AssetsResources.VIP2,
    AssetsResources.VIP3,
    AssetsResources.VIP4,
    AssetsResources.VIP5,
    AssetsResources.w1,
    AssetsResources.w2,
  ];

I understand I can use method:

final bool isVIP = avatarList[index].contains('VIP');

But since AssetsResources.VIP1 is not a String like 'VIP1'but a path from other dart file, so here I have no idea how to check if the element from avatarList contains VIP value, thanks for any clue!

Update

Thanks guys for the help and sorry I didnt describe clearly, what I mean is, if

  List idealList = [
    'vip1',
    'vip2',
    'vip3',
    'vip4',
    'vip5',
  ];

so the elements in the idealList is 'vip1' but in my case the list myList is

  List myList = [
    AssetsResources.VIP1,
    AssetsResources.VIP2,
    AssetsResources.VIP3,
    AssetsResources.VIP4,
    AssetsResources.VIP5,
    AssetsResources.w1,
    AssetsResources.w2,
  ];

So it seems I can not directly use some methode as follows

final bool isVIP = myList[index].contains('VIP');

since the elements from myList is just a path(sorry I dont know how to call this value), could you please let me know in my case how to check if this path contains 'VIP' value? thanks!

Update yes, AssetsResources is very simple, just store the asset path:

class AssetsResources {
  /*worm avatar*/
  static const String VIP1 = 'assets/worms/VIP_1.svg';
  static const String VIP2 = 'assets/worms/VIP_2.svg';
  static const String VIP3 = 'assets/worms/VIP_3.svg';
  static const String VIP4 = 'assets/worms/VIP_4.svg';

}

CodePudding user response:

The code should work fine :

class AssetsResources {
  /*worm avatar*/
  static const String VIP1 = 'assets/worms/VIP_1.svg';
  static const String VIP2 = 'assets/worms/VIP_2.svg';
  static const String VIP3 = 'assets/worms/VIP_3.svg';
  static const String VIP4 = 'assets/worms/VIP_4.svg';
}

void main() {
  List myList = [
    AssetsResources.VIP1,
    AssetsResources.VIP2,
    AssetsResources.VIP3,
    AssetsResources.VIP4,
  ];

  for (final asset in myList) {
    print(asset);
    print(asset.contains('VIP'));
  }
}

The above prints :

assets/worms/VIP_1.svg
true
assets/worms/VIP_2.svg
true
assets/worms/VIP_3.svg
true
assets/worms/VIP_4.svg
true

CodePudding user response:

If I understood you correctly.

void main() {
  for(var i = 0; i < avatarList.length; i  ) {
    String element = avatarList[i];
    
    if(element.contains('VIP')) {
      print(other.contains(element)); // true
      print(other.firstWhere((e) => e.contains(element))); // 'VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5'
    }
  }
}

List<String> avatarList = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
List<String> other = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
  • Related