Slide 14
Slide 14 text
補足:会計科目用のアイテム表示処理に関する処理例
選択履歴をMergeする処理とSortする処理を組み合わせる事で実現する
final masterItemsProvider = FutureProvider.family, String>((ref, masterType) async {
final repository = ref.watch(masterRepositoryProvider);
final items = await repository.getMasterItems(masterType);
final history = await repository.getSelectionHistory();
// 選択履歴をマージする処理
// 処理の流れ:
// 1. 各itemに対して、履歴の中から同じID・タイプのitemを探す
// 2. 見つかった場合、その履歴のlastSelected情報を使用
// 3. 見つからない場合、元のitemをそのまま使用
// ソート処理: 最近選択したアイテムを先頭に並べる
// sort()メソッド: リストを並び替えます
// 比較関数は、2つの要素a, bを比較し、以下を返します:
// - 負の数: aがbより前に来る
// - 0: 順序は変わらない
// - 正の数: bがaより前に来る
// 処理済みのリストを返す
return itemsWithHistory;
});
final itemsWithHistory = items.map((item) {
final historyItem = history.firstWhere(
(h) => h.id == item.id && h.masterType == item.masterType,
orElse: () => item,
);
return historyItem.lastSelected != null ? historyItem : item;
}).toList();
itemsWithHistory.sort((a, b) {
if (a.lastSelected != null && b.lastSelected == null) return -1;
if (a.lastSelected == null && b.lastSelected != null) return 1;
if (a.lastSelected != null && b.lastSelected != null) {
return b.lastSelected!.compareTo(a.lastSelected!);
}
return a.name.compareTo(b.name);
});
FutureProvoder.familyを利用した処理
Merge処理
Sort処理