RP: 処理速度の最適化 - Ki-Kobayashi/flutter_wiki GitHub Wiki

🟩 Listを最適化する

🟡 考え方: 

ListTileを作成する際、items[index]などを引数に渡さないようにしたい (const をつけられるようにしたい)   👉 引数を渡す = constがつけられない = リビルド時再描画対象になってしまう = 不要に再描画しパフォーマンスが落ちる

🟡 コード

/// 💎1つあたりのListTileに格納したいデータを取得するプロバイダー(🛑UnimplementedErrorとし、後でオーバーライドする)
@Riverpod(dependencies: []) 👈このプロバイダーに依存するものはないことを明示しないと、Lint警告がでてしまう
String currentItem(CurrentItemRef ref) {
  throw UnimplementedError();
}


***

……
……
……
return ListView(
  children: [
    for (final item in items)
   /// 💎 currentItemProvider を格納したいデータで上書きする
      ProviderScope(
        overrides: [currentItemProvider.overrideWithValue(item)]
        child: const MyListTile(),
      ),
  ],

);

***


class MyListTile extends ConsumerWidget {
   const MyListTile({ supuer.key });

   @override
   Widget build(BuildContext context, WidgetRef ref) {
     final item = ref.watch(currentItemProvider); 👈上書き後のデータを取得
       return ListTile(
            ...
       );
   }

}

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.

🟩

🟡

.