Create Flutter Freezed Class
1 min read

Create Flutter Freezed Class

To create Freezed class, use the frf short code in Visual Studio Code (Using Freezed extension). The code should look like this.

import 'package:freezed_annotation/freezed_annotation.dart';

part 'checkin_activity.freezed.dart';
part 'checkin_activity.g.dart';

@freezed
class CheckinActivity with _$CheckinActivity {

  factory CheckinActivity() = _CheckinActivity;

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

Run the build_runner command to generate the part files.

flutter pub run build_runner build

To create a property, specify it in the constructor.

import 'package:freezed_annotation/freezed_annotation.dart';

part 'checkin_activity.freezed.dart';
part 'checkin_activity.g.dart';

@freezed
class CheckinActivity with _$CheckinActivity {
  factory CheckinActivity({required String title}) = _CheckinActivity;

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