Freezed: fromJson/toJson don't work

Created on 14 Jun 2020  路  3Comments  路  Source: rrousselGit/freezed

my model

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import 'package:make_sleep_better/src/model/entities/data.dart';

part 'feedback_state.freezed.dart';
part 'feedback_state.g.dart';

@freezed
abstract class FeedbackState with _$FeedbackState {
  const factory FeedbackState({List<Data> listDataWakeUp}) = _FeedbackState;

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

compile error

lib/src/presentation/screens/feedback/feedback_state.dart:6:6: Error: Error when reading 'lib/src/presentation/screens/feedback/feedback_state.g.dart': No such file or directory
part 'feedback_state.g.dart';
     ^
lib/src/presentation/screens/feedback/feedback_state.dart:6:6: Error: Can't use 'lib/src/presentation/screens/feedback/feedback_state.g.dart' as a part, because it has no 'part of' declaration.
part 'feedback_state.g.dart';
     ^
lib/src/presentation/screens/feedback/feedback_state.freezed.dart:98:7: Error: Method not found: '_$_$_FeedbackStateFromJson'.
      _$_$_FeedbackStateFromJson(json);
      ^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/src/presentation/screens/feedback/feedback_state.freezed.dart:136:12: Error: The method '_$_$_FeedbackStateToJson' isn't defined for the class '_$_FeedbackState'.
 - '_$_FeedbackState' is from 'package:make_sleep_better/src/presentation/screens/feedback/feedback_state.dart' ('lib/src/presentation/screens/feedback/feedback_state.dart').
Try correcting the name to the name of an existing method, or defining a method named '_$_$_FeedbackStateToJson'.
    return _$_$_FeedbackStateToJson(this);
           ^^^^^^^^^^^^^^^^^^^^^^^^

All 3 comments

Do you have json_serialisable package installed?

From that error message, perhaps its not being called when you run the builders? as the error seems to suggest feedback_state.g.dart was not generated at all ?

Do you see any errors when you run flutter pub pub run build_runner build --delete-conflicting-outputs ?

Hi @maks.
flutter pub pub run build_runner build --delete-conflicting-outputs run without error

[INFO] Generating build script...
[INFO] Generating build script completed, took 423ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 82ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 739ms

[INFO] Running build...
[INFO] Running build completed, took 14ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 62ms

[INFO] Succeeded after 88ms with 0 outputs (0 actions)

pubspec

name: make_sleep_better
description: A new Flutter application.

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.1.0+9

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  intl: ^0.16.1
  cupertino_icons: ^0.1.3
  # For state management
  state_notifier: ^0.5.0
  flutter_state_notifier: ^0.4.2
  provider: ^4.1.3
  # For freezed object
  freezed_annotation: ^0.11.0
  #  firebase_database: ^3.1.1
  shared_preferences: ^0.5.7+3
  animations: ^1.1.0
  path_provider: ^1.6.10
  url_launcher: ^5.4.10
  flutter_local_notifications: ^1.4.4+1
  android_intent: ^0.3.7+2
  logger: ^0.9.1
  lazy_code: ^0.1.1+1



dev_dependencies:
  flutter_launcher_icons: ^0.7.5
  build_runner: ^1.10.0
  freezed: ^0.11.0
#  pedantic: ^1.8.0+1
  flutter_test:
    sdk: flutter

flutter_icons:
  #  image_path: "assets/images/icon-128x128.png"
  image_path_android: "assets/images/icon.png"
  image_path_ios: "assets/images/icon.png"
  android: "ic_launcher" #true # can specify file name here e.g. "ic_launcher"
  ios: "ic_launcher" #true # can specify file name here e.g. "My-Launcher-Icon"
flutter:
  uses-material-design: true

I'm done after install json_serializable and json_annotation :D

Was this page helpful?
0 / 5 - 0 ratings