0

I am new to flutter and after searching everywhere I couldn't find the reason behind this error

Below is my code

// ignore: file_names
import 'package:freezed_annotation/freezed_annotation.dart';
part 'NetworkResponse.freezed.dart';
@freezed
  class NetworkResponse with _$NetworkResponse {
    const factory NetworkResponse.success(Map<String, dynamic> data) = OK;
    const factory NetworkResponse.error(String message) = ERROR;
    const factory NetworkResponse.loading(String message) = LOADING;
}

Error

enter image description here

1) Target of URI doesn't exist: 'NetworkResponse.freezed.dart'.
2) Classes can only mix in mixins and classes.dartmixin_of_non_class
3) The name 'OK' isn't a type and can't be used in a redirected constructor.
Try redirecting to a different constructor.

Code Source: https://betterprogramming.pub/building-generic-and-performant-networking-layer-in-flutter-b25c2b1b89a4

Nick
  • 1,127
  • 2
  • 15
  • 40
  • 1
    You must have the build_runner package installed in your project and then build it as described [here in the freezed](https://github.com/rrousselGit/freezed) package readme. Ex `flutter pub run build_runner build` – Chance Jul 31 '22 at 21:35
  • Yes its already there – Nick Jul 31 '22 at 21:37

1 Answers1

2

In order for freezed generator to work, yours file names have to match.

I suspect that your file is not named NetworkResponse.dart since it is not a flutter convention. Flutter convention is to name files with snake case - network_response.dart. In this case, your freezed file would be network_response.freezed.dart and it should be the string following the part keyword.

With that implemented, you only have to run flutter pub run build_runner build. In case of any errors, --delete-conflicting-outputs flag should do the job. Also make sure to not only have freezed_annotation dependency in your pubspec.yaml but also freezed itself as a dev dependency.

Wiktor
  • 726
  • 5
  • 20