dartの基本クラスを便利に使えるようにするライブラリらしい
googleのできるエンジニアが使っていると思われる。
https://pub.dev/packages/collection
import 'package:collection/collection.dart'; void main() { // Listの拡張機能 List<int> numbers = [1, 2, 3, 4, 5]; print(numbers.containsAll([1, 2])); // true print(numbers.indexOfFirst((n) => n > 3)); // 3 // Setの拡張機能 Set<int> set1 = {1, 2, 3}; Set<int> set2 = {3, 4, 5}; print(set1.union(set2)); // {1, 2, 3, 4, 5} // Mapの拡張機能 Map<String, int> ages = {'Alice': 30, 'Bob': 25}; print(ages.containsKey('Alice')); // true print(ages.mapValues((key, value) => value + 1)); // {Alice: 31, Bob: 26} // Equality List<int> list1 = [1, 2, 3]; List<int> list2 = [1, 2, 3]; print(ListEquality().equals(list1, list2)); // true // Grouping List<String> words = ['apple', 'banana', 'apricot']; var grouped = groupBy(words, (String word) => word[0]); print(grouped); // {a: [apple, apricot], b: [banana]} }
dartは、直接Map型を継承できないそうだが、以下のクラスを(委譲パターンで)つくるとできる。1クッションおくかんじだ。
class Collection implements Map<String, dynamic> { final Map<String, dynamic> _internalMap = {}; @override dynamic operator [](Object? key) => _internalMap[key]; @override void operator []=(String key, dynamic value) { _internalMap[key] = value; } @override void addAll(Map<String, dynamic> other) { _internalMap.addAll(other); } @override void addEntries(Iterable<MapEntry<String, dynamic>> newEntries) { _internalMap.addEntries(newEntries); } @override Map<RK, RV> cast<RK, RV>() => _internalMap.cast<RK, RV>(); @override void clear() { _internalMap.clear(); } @override bool containsKey(Object? key) => _internalMap.containsKey(key); @override bool containsValue(Object? value) => _internalMap.containsValue(value); @override Iterable<MapEntry<String, dynamic>> get entries => _internalMap.entries; @override void forEach(void Function(String key, dynamic value) action) { _internalMap.forEach(action); } @override bool get isEmpty => _internalMap.isEmpty; @override bool get isNotEmpty => _internalMap.isNotEmpty; @override Iterable<String> get keys => _internalMap.keys; @override int get length => _internalMap.length; @override dynamic putIfAbsent(String key, dynamic Function() ifAbsent) { return _internalMap.putIfAbsent(key, ifAbsent); } @override dynamic remove(Object? key) { return _internalMap.remove(key); } @override void removeWhere(bool Function(String key, dynamic value) test) { _internalMap.removeWhere(test); } @override dynamic update(String key, dynamic Function(dynamic value) update, {dynamic Function()? ifAbsent}) { return _internalMap.update(key, update, ifAbsent: ifAbsent); } @override void updateAll(dynamic Function(String key, dynamic value) update) { _internalMap.updateAll(update); } @override Iterable<dynamic> get values => _internalMap.values; @override Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> Function(String key, dynamic value) convert) { return _internalMap.map(convert); } @override String toString() => _internalMap.toString(); }
JsonFile?クラスをCollectionクラスから継承
import 'package:flutter/services.dart'; import 'dart:convert'; class JsonFile extends Collection { final String filename; JsonFile(this.filename); Future<void> readJson() async { try { final jsonString = await rootBundle.loadString('assets/$filename'); final jsonMap = json.decode(jsonString) as Map<String, dynamic>; addAll(jsonMap); } catch (e) { print('エラー: $e'); rethrow; } } } void main() async { JsonFile jsonFile = JsonFile('example.json'); // JSONファイルを読み込み await jsonFile.readJson(); // JSONデータにアクセス print(jsonFile['someKey']); // JSONデータをダンプ print(jsonFile.toString()); }