Open40

【Flutter×Firebase】個人アプリ開発スクラップ_ Flutter Hooks, Freezed, Riverpod, State_notifier

ピン留めされたアイテム
RereRere

概要

Flutter×Firebaseを使ったアプリ開発で

  • 調べたこと
  • 疑問点
  • 学び・気づき
    をひたすらメモしていく。

アプリ全体設計

  • Flutter
    • Riverpod, State_notifier,Freezedで状態管理
  • Firebase
    • Authで認証管理
    • Firestoreでトレーニングログ管理
    • Cloud functionsでログをデータ整形→Firestoreで管理
    • Remote configでバージョン管理(強制アップデート)
    • Analyticsで利用状況/イベント管理
    • Crashlyticsでクラッシュ監視
  • Codemagic
    • CI/CD

▷BIG3記録管理アプリ「LifterLog」

RereRere

VSCodeの設定

Snippet

Android Studio→VSCodeに乗り換える際に設定したsnippet。
Freezed, Reiverpod, State_notifierでよく使うものだけを一旦。

本当は正規表現使って、ファイル名からクラス名を自動生成したいけどそれはまた今後。
(ファイル名から"_",と拡張子を削除→Upper Camel Caseに変換)

参考記事

dart.json
dart.json

{
	"Freezed Class": {
		"prefix": "freezed_class",
		"body": [
			"import 'package:freezed_annotation/freezed_annotation.dart';",
			"part '${TM_FILENAME_BASE}.freezed.dart';",
			"part '${TM_FILENAME_BASE}.g.dart';",
			"",
			"@freezed",
			"abstract class ${1:CLASS_NAME} with _$${1:CLASS_NAME}{",
			"const factory ${1:CLASS_NAME}({",
			"}) = _${1:CLASS_NAME};",
			"",
			" factory ${1:CLASS_NAME}.fromJson(Map<String, dynamic> json) => _$${1:CLASS_NAME}FromJson(json);",
			"}"
		],
		"description": "Freezed Class"
	},
	"State_notifier": {
		"prefix": "state_notifier",
		"body": [
			"import 'package:hooks_riverpod/hooks_riverpod.dart';",
			"import 'package:state_notifier/state_notifier.dart';",
			"",
			"class ${1:CLASS_NAME} extends StateNotifier<${2:STATE_NAME}> {",
			" ${1:CLASS_NAME}() : super();",
			"}"
		],
		"description": "Freezed Class"
	},
	"Riverpod_stateNotifier": {
		"prefix": "riverpod_state_notifier",
		"body": [
			"final ${1:PROVIDER_NAME} = StateNotifierProvider<${2:STAETE_NAME}, ${3:CALLBACK_NAME}>(",
			" (ref) => ${4:SOME_ACTIONS}",
			");",
		],
		"description": "Freezed Class"
	},
	"Riverpod_streamNotifier": {
		"prefix": "riverpod_stream_autoDispose",
		"body": [
			"final ${1:PROVIDER_NAME} = StreamProvider.autoDispose<${2:CALLBACK_NAME}>(",
			" (ref) {",
			" ${3:SOME_ACTIONS}",
			  "}",
			");",
		],
		"description": "Freezed Class"
	},
}

拡張機能

こちらの記事を参考
https://zenn.dev/hagakun_dev/articles/2f2eb65b892bea

RereRere

fromJson()/toJsonでのTimeStamp←→DateTimeの変換

こちらの記事とmonoさんのコードを参考にjson_converterを設定して対応。
https://qiita.com/tetsufe/items/3c7d997f1c13c659628c#comment-5ce93b14ddfbc9ee6428
https://github.com/mono0926/flutter_firestore_ref/blob/main/lib/src/json_converter.dart

json_converter.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:json_annotation/json_annotation.dart';

class TimestampConverter implements JsonConverter<DateTime?, Timestamp?> {
  const TimestampConverter();

  
  DateTime? fromJson(Timestamp? json) => json?.toDate();

  
  Timestamp? toJson(DateTime? object) =>
      object == null ? null : Timestamp.fromDate(object);
}

使用例↓

example_state.dart

abstract class ExampleState with _$ExampleState {
  const factory ExampleState({
    required () DateTime createDate,
    required () DateTime updateDate,
  }) = _ExampleState;

  factory ExampleState.fromJson(Map<String, dynamic> json) =>
      _$ExampleStateFromJson(json);
}
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
Hidden comment
RereRere

メモにはScrapboxがいいかも。
notion等のような階層構造ではなく、wikiのように双方向リンクを形成する。

https://scrapbox.io/

RereRere
CocoaPods could not find compatible versions for pod "flutter_facebook_auth":
  In Podfile:
    flutter_facebook_auth (from `.symlinks/plugins/flutter_facebook_auth/ios`)

Specs satisfying the `flutter_facebook_auth (from `.symlinks/plugins/flutter_facebook_auth/ios`)` dependency were found, but they required a higher minimum deployment target.

https://stackoverflow.com/questions/52398435/cocoapods-could-not-find-compatible-versions-for-pod-firebase-core-cloud-fir

flutter_facebook_authがios>11以上に指定されていたので、Podfileの platform :ios, を11にして対応。