Reputation: 7108
I'm using Freezed
to generate data-class on my flutter project.
I did everything exactly like mentioned in the package readme:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'access_token.freezed.dart';
@freezed
class AccessToken with _$AccessToken {
@JsonSerializable()
const factory AccessToken(
@JsonKey(name: 'access_token') String accessToken,
@JsonKey(name: 'refresh_token') String refreshToken,
) = _AccessToken;
factory AccessToken.fromJson(Map<String, Object?> json) =>
_$AccessTokenFromJson(json);
}
The build completes successfully.
When I run the app I'm getting:
lib/services/models/access_token.freezed.dart:118:7: Error: Method not found: '_$$_AccessTokenFromJson'. _$$AccessTokenFromJson(json); ^^^^^^^^^^^^^^^^^^^^^^^ lib/services/models/access_token.freezed.dart:157:12: Error: The method '$$AccessTokenToJson' isn't defined for the class '$_AccessToken'.
- '_$AccessToken' is from 'package:tenant_app/services/models/access_token.dart' ('lib/services/models/access_token.dart'). Try correcting the name to the name of an existing method, or defining a method named '$$_AccessTokenToJson'. return _$$_AccessTokenToJson(
Why Freezed
didn't generate that function correctly? What am I missing?
Upvotes: 5
Views: 5830
Reputation: 2263
In general, you need to add:
part 'access_token.g.dart';
This aspect is mentioned in the official package page, on pub.dev.
// This file is "main.dart"
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
// required: associates our `main.dart` with the code generated by Freezed
part 'main.freezed.dart';
// optional: Since our Person class is serializable, we must add this line.
// But if Person was not serializable, we could skip it.
part 'main.g.dart';
@freezed
class Person with _$Person {
const factory Person({
required String firstName,
required String lastName,
required int age,
}) = _Person;
factory Person.fromJson(Map<String, Object?> json)
=> _$PersonFromJson(json);
}
Upvotes: 0
Reputation: 8597
You have to add the following part:
part 'access_token.g.dart';
And you don't need the following:
@JsonSerializable()
And make sure you run (using build
or watch
below):
flutter pub run build_runner build --delete-conflicting-outputs
I took your example and successfully generated everything using:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'access_token.freezed.dart';
part 'access_token.g.dart';
@freezed
class AccessToken with _$AccessToken {
const factory AccessToken(
@JsonKey(name: 'access_token') String accessToken,
@JsonKey(name: 'refresh_token') String refreshToken,
) = _AccessToken;
factory AccessToken.fromJson(Map<String, Object?> json) => _$AccessTokenFromJson(json);
}
Using freezed_annotation: ^2.1.0
, freezed: ^2.1.0+1
, build_runner: ^2.2.0,
json_annotation: ^4.6.0
, json_serializable: ^6.3.1
Make sure to check that those are included (according to OP in comment to this answer, packages was missing).
Generated .g.dart file:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'access_token.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$_AccessToken _$$_AccessTokenFromJson(Map<String, dynamic> json) =>
_$_AccessToken(
json['access_token'] as String,
json['refresh_token'] as String,
);
Map<String, dynamic> _$$_AccessTokenToJson(_$_AccessToken instance) =>
<String, dynamic>{
'access_token': instance.accessToken,
'refresh_token': instance.refreshToken,
};
Upvotes: 12
Reputation: 1404
is it null safety...? if yes use ? in all fields
and also missed .g.dart'; path
i will give an example
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
part 'comments.model.freezed.dart';
part 'comments.model.g.dart'; // missing
@freezed
@immutable
class CommentsModel with _$CommentsModel {
const CommentsModel._();
const factory CommentsModel({
int? id,
@JsonKey(name: 'x_post_id') int? xPostId,
@JsonKey(name: 'x_user_id') int? xUserId,
@JsonKey(name: 'x_body') String? xBody,
@JsonKey(name: 'comment_likes_count') int? commentLikesCount,
@JsonKey(name: 'comment_like_by_user_count') int? commentLikeByUserCount,
@JsonKey(name: 'created_at') DateTime? createdAt,
@JsonKey(name: 'updated_at') DateTime? updatedAt,
}) = _CommentsModel;
factory CommentsModel.fromJson(Map<String, dynamic> json) =>
_$CommentsModelFromJson(json);
}
to run build_runner use this also
flutter packages pub run build_runner watch --delete-conflicting-outputs
Upvotes: 0
Reputation: 1230
Can you try with flutter pub run build_runner build --delete-conflicting-outputs
Upvotes: 0