【Flutter】Riverpodインストール時のテンプレート【メモ】

必要プラグイン

以下のコマンドを実行してプラグインをインストールします。

flutter pub add freezed_annotation
flutter pub add build_runner --dev
flutter pub add freezed --dev
flutter pub add json_serializable --dev
flutter pub add json_annotation
flutter pub add flutter_riverpod

Model作成のためのソーステンプレート

Modelを作成する際は、以下のソースをコピーして’template’, ‘Template’を任意のファイル名およびクラス名に変更します。

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'template.freezed.dart';
part 'template.g.dart';

@freezed
class Template with _$Template {
  const factory Template({
    required String title,
  }) = _Template;

  factory Template.fromJson(Map<String, dynamic> json) =>
      _$TemplateFromJson(json);
}

freezedファイル, gファイルの生成

以下のコマンドを実行してModelから他の必要ファイルを生成します。

  • 通常
flutter pub run build_runner build
  • 強制
flutter pub run build_runner build --delete-conflicting-outputs
  • 動的
flutter pub run build_runner watch