0
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?

flutter build_runner で freezed でmodelを作成するときに起きる、getter がないよってエラー の一時的解決方法

Last updated at Posted at 2025-03-09

エラー内容

Missing concrete implementations of '_$Post.toJson', 'getter _$Post.body', 'getter _$Post.createdAt', 'getter _$Post.imageUrl', and 3 more.
Try implementing the missing methods, or make the class abstract.

エラーが出たファイル

/// post.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:riverpod_training/function/timestamp_converter.dart';

part 'post.freezed.dart';
part 'post.g.dart';

@freezed
class Post with _$Post {
  factory Post({
    required String body,
    required String userId,
    required String postId,
    required String imageUrl,
    @TimestampConverter() required Timestamp createdAt,
    @TimestampConverter() required Timestamp updatedAt,
  }) = _Post;

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

エラー再現

post.dartを作成して、ターミナルで以下のコードを叩く

flutter pub run build_runner build --delete-conflicting-outputs

その結果、post.freezed.dartpost.g.dartが作成されるが、先ほどのエラーが発生する。

解決方法

post.freezed.dartを以下のように書き加える

...
// dart format off
T _$identity<T>(T value) => value;

+ final _privateConstructorUsedError = UnsupportedError(
+   'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models',
+ );

/// @nodoc
mixin _$Post {

- String get body; String get userId; String get postId; String get imageUrl;@TimestampConverter() Timestamp get createdAt;@TimestampConverter() Timestamp get updatedAt;
+ String get body => throw _privateConstructorUsedError; String get userId => throw _privateConstructorUsedError; String get postId => throw _privateConstructorUsedError; String get imageUrl => throw _privateConstructorUsedError;@TimestampConverter() Timestamp get createdAt => throw _privateConstructorUsedError;@TimestampConverter() Timestamp get updatedAt => throw _privateConstructorUsedError;
/// Create a copy of Post
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$PostCopyWith<Post> get copyWith => _$PostCopyWithImpl<Post>(this as Post, _$identity);

  /// Serializes this Post to a JSON map.
-    Map<String, dynamic> toJson();
+    Map<String, dynamic> toJson() => throw _privateConstructorUsedError;


0
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
0
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?