Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezed model throws error on code generation #268

Closed
vedantrathore opened this issue Sep 22, 2020 · 7 comments
Closed

Freezed model throws error on code generation #268

vedantrathore opened this issue Sep 22, 2020 · 7 comments
Labels
bug Something isn't working needs triage

Comments

@vedantrathore
Copy link

Describe the bug
Unable to generate freezed classes for models.

To Reproduce

I have this model file named activity.dart. I've highlighted the errors I'm getting from VSCode in the comments.

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'activity.freezed.dart'; // Expected this library to be part of 'package:habitwave/app/model/Activity.dart', not 'package:habitwave/app/model/activity.dart'. Try including a different part, or changing the name of the library in the part's part-of directive
part 'activity.g.dart'; // Expected this library to be part of 'package:habitwave/app/model/Activity.dart', not 'package:habitwave/app/model/activity.dart'. Try including a different part, or changing the name of the library in the part's part-of directive

enum ActivityType {
  AcceptAddToSquadRequest,
  RemovedFromSquad,
  AddToSquadRequest,
  Reaction,
  Nudge,
}

toNull(_) => null;

@freezed
abstract class ActivityObject with _$ActivityObject {
  const factory ActivityObject({
    String sentByName,
    String requestId,
    int sentOn,
    String id,
    ActivityType activityType,
    String sentById,
    bool unread,
    String reactionType,
    int reactionCount,
    String habitName,
    String text,
    String sentByPhotoURL,
  }) = _ActivityObject; // The redirected constructor '_ActivityObject Function({ActivityType activityType, String habitName, String id, int reactionCount, String reactionType, String requestId, String sentById, String sentByName, int sentOn, String text, bool unread})' has incompatible parameters with 'ActivityObject Function({ActivityType activityType, String habitName, String id, int reactionCount, String reactionType, String requestId, String sentById, String sentByName, String sentByPhotoURL, int sentOn, String text, bool unread})'. Try redirecting to a different constructor

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

@freezed
abstract class Activity implements _$Activity {
  const Activity._();
  const factory Activity({
    List<ActivityObject> data,
    dynamic metadata,
  }) = _Activity;

  Activity add(Activity other) {
    return Activity(metadata: other.metadata, data: this.data + other.data);
  }

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

It's a simple freezed class. To circumvent the first dart part_of_different_library issue, I've to rename the file to Activity.dart after running flutter pub run build_runner build --delete-conflicting-outputs

Haven't found a solution to the second constructor issue. Do let me know if I'm missing something here. I have similar 7-8 model classes and they all seem to be working fine.

Expected behavior
Freezed and JSONSerializable should generate 2 files without error. activity.g.dart and activity.freezed.dart

@vedantrathore vedantrathore added bug Something isn't working needs triage labels Sep 22, 2020
@vedantrathore
Copy link
Author

vedantrathore commented Sep 22, 2020

Update: I seem to have solved the second issue by deleting the 2 generated files and running the build runner again.

Error: The redirected constructor '_ActivityObject Function({ActivityType activityType, String habitName, String id, int reactionCount, String reactionType, String requestId, String sentById, String sentByName, int sentOn, String text, bool unread})' has incompatible parameters with 'ActivityObject Function({ActivityType activityType, String habitName, String id, int reactionCount, String reactionType, String requestId, String sentById, String sentByName, String sentByPhotoURL, int sentOn, String text, bool unread})'. Try redirecting to a different constructor

The first one is still a bummer as I have to rename the file everytime after running the build runner.

@vedantrathore
Copy link
Author

As we can see I'm getting this error in the main model file
Screen Shot 2020-09-22 at 8 41 25 PM

but the part files in the generated files seem to be correct
Screen Shot 2020-09-22 at 8 43 33 PM

Screen Shot 2020-09-22 at 8 43 51 PM

@joanofdart
Copy link

I'm having these issues constantly too and the only solution is to close-reopen vscode.

@rrousselGit
Copy link
Owner

Do you have an error in the terminal, or is it just the IDE?
If so, you may want to raise an issue to vscode as that's unrelated to Freezed

If that's what I'm thinking about, the way I go around this is using cmd + hovering the "part"

@KlausJokisuo
Copy link

This might be related dart-lang/sdk#43166

@vedantrathore
Copy link
Author

@rrousselGit I think it was just IDE but due the error I was unable to compile the app. This might've been the weirdest bug I've encountered. To bypass this I eventually had to change the name of the file from activity.dart to activityFeed.dart and the error vanished. Super weird. I think you can close this issue for now given that it might not be an issue with the package itself.

@al-mohad
Copy link

The class 'x' doesn't have a constructor named '_'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

5 participants