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
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.
As we can see I'm getting this error in the main model file

but the part files in the generated files seem to be correct


I'm having these issues constantly too and the only solution is to close-reopen vscode.
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"
This might be related https://github.com/dart-lang/sdk/issues/43166
@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.
Most helpful comment
This might be related https://github.com/dart-lang/sdk/issues/43166