Created
October 26, 2020 16:34
-
-
Save JosLuna98/51490caca1524826d9085fda19c801b8 to your computer and use it in GitHub Desktop.
[Dart] Game filter for question in StackOverFlow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://stackoverflow.com/questions/64529394/flutter-dart-how-to-remove-excepted-element-in-list-in-map | |
class PlatformFilter { | |
bool isSelected; | |
int id; | |
PlatformFilter(this.id, this.isSelected); | |
} | |
class Game { | |
int id; | |
//String name... | |
List<Platform> platforms; | |
Game(this.id, this.platforms); | |
@override | |
String toString() => 'gameId: $id, platforms:${platforms.namesString()}'; | |
} | |
class Platform { | |
String name; | |
int id; | |
//IconData icon; | |
//Other view value... | |
Platform(this.id, this.name); | |
} | |
extension on List<Platform> { | |
String namesString() { | |
String values = '['; | |
this.asMap().forEach((index, element) => values += | |
(index != this.length - 1) ? element.name + ',' : element.name + ']'); | |
return values; | |
} | |
} | |
Map<int, List<Game>> filtering() { | |
List<PlatformFilter> filters = [ | |
PlatformFilter(1, true), //Xbox | |
PlatformFilter(2, true), //Playstation | |
PlatformFilter(3, true), //Switch | |
PlatformFilter(4, false), //Android | |
PlatformFilter(5, false), //Mac | |
PlatformFilter(6, false), //Linux | |
]; | |
Map<int, List<Game>> data = { | |
1: [ | |
Game(1, [ | |
Platform(1, 'Xbox'), | |
Platform(2, 'Playstation'), | |
Platform(3, 'Switch') | |
]), | |
Game(2, | |
[Platform(1, 'Xbox'), Platform(3, 'Switch'), Platform(4, 'Android')]), | |
Game(3, | |
[Platform(4, 'Android'), Platform(5, 'Mac'), Platform(6, 'Linux')]), | |
], | |
2: [ | |
Game(4, [ | |
Platform(2, 'Playstation'), | |
Platform(3, 'Switch'), | |
Platform(4, 'Android') | |
]), | |
Game( | |
5, [Platform(1, 'Xbox'), Platform(4, 'Android'), Platform(5, 'Mac')]), | |
], | |
3: [ | |
Game(6, [Platform(5, 'Mac')]), | |
], | |
4: [ | |
Game(7, [Platform(5, 'Mac'), Platform(4, 'Android')]), | |
], | |
5: [ | |
Game(8, [Platform(3, 'Switch')]), | |
], | |
}; | |
Map<int, List<Game>> result = {}; | |
data.forEach((key, group) { | |
group.asMap().forEach((index, game) { | |
game.platforms.forEach((platform) { | |
filters.forEach((element) { | |
if (element.id == platform.id && element.isSelected) { | |
result.putIfAbsent(key, () => List()); | |
if (!result[key].contains(game)) { | |
result[key].add(game); | |
} | |
} | |
}); | |
}); | |
}); | |
}); | |
return result; | |
} | |
void main() { | |
var result = filtering(); | |
result.forEach((key, value) { | |
print('$key:'); | |
value.forEach((element) => print('\t$element')); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment