1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

freezedのwhenとかwhenOfNullの使い方

1
Posted at

Freezedクラスの定義

@freezed
class Dummy with _$Dummy {
factory Dummy.dog(
    required String? name,
    required int? age,
)

factory Dummy.cat(
    required String? name,
    required int? age,
    required String? color,
)

// whenメソッドの引数の型がfactoryコンストラクタの名前に一致する
// 下の関数はwhenの例を示すだけの意味のないもの
String get name => when(
    dog : (_, _) => 'POCHI',
    cat : (_, _, _) => 'TAMA',
)

// whenOrNullであれば引数を限定することができる
String get name => whenOrNull(
    dog : (_, _) => 'POCHI',
)


}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?