Flutter Freezed Method not found
Asked Answered
F

4

5

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?

Fleisig answered 24/8, 2022 at 11:14 Comment(1)
Problem solved. I accidentally forgot to add the json_annotation and json_serializable to my project dependences.Fleisig
A
12

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,
    };
Arielariela answered 24/8, 2022 at 11:46 Comment(1)
I accidentally forgot to add the json_annotation and json_serializable to my project dependences. Problem solved.Fleisig
G
0

Can you try with flutter pub run build_runner build --delete-conflicting-outputs

Giuditta answered 24/8, 2022 at 11:21 Comment(0)
S
0

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

Supplant answered 24/8, 2022 at 11:42 Comment(7)
The problem is that it does not generate the .g.dart file. So I removed it.Fleisig
? in all fieldsSupplant
then run this flutter packages pub run build_runner watch --delete-conflicting-outputsSupplant
add this 'access_token.g.dart';Supplant
Does it generates the .g.dart file for you?Fleisig
Null safety have nothing to do with it.Arielariela
there is a possibility of a accessToken getting null , it's datatype is String not String? so i mention it to handle that issue tooSupplant
S
0

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);
}
Someway answered 9/2, 2024 at 8:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.