freezed icon indicating copy to clipboard operation
freezed copied to clipboard

Import alias v2

Open rrousselGit opened this issue 3 years ago • 11 comments

This bug is marked as closed, but as of 18th Sep. 2021 and version 0.14.5 - it is not (fully fixed). Example:

/// importing library from another package with prefix
import 'package:some_package/core.dart' as core;

@freezed
class ProfileEvent with _$ProfileEvent {
  /// note that core.Profile is _also_ a freezed class
  const factory ProfileEvent.changeTo({required core.Profile profile}) =
      _ChangeTo;
}

Now, this will generate the following syntax errors in .freezed file:

abstract class _$ChangeToCopyWith<$Res> {
  factory _$ChangeToCopyWith(_ChangeTo value, $Res Function(_ChangeTo) then) =
      __$ChangeToCopyWithImpl<$Res>;
  $Res call({core.Profile profile}); // note: generator adds prefix correctly here

  $ProfileCopyWith<$Res> get profile; // syntax error: Undefined class '$ProfileCopyWith'.
}
class __$ChangeToCopyWithImpl<$Res> extends _$ProfileEventCopyWithImpl<$Res>
    implements _$ChangeToCopyWith<$Res> {
/// [ .. lines removed .. ]
  @override
  $ProfileCopyWith<$Res> get profile { // Syntax error: Undefined class '$ProfileCopyWith'.
    return $ProfileCopyWith<$Res>(_value.profile, (value) { // same syntax error as above
      return _then(_value.copyWith(profile: value));
    });
  }

Currently, my workaround is to edit the .freezed files manually adding the prefix everywhere where there are syntax errors:

core.$ProfileCopyWith<$Res>

@rrousselGit : Please advise if I should open a new bug for this, alternatively, this bug probably should be repoened.

Originally posted by @matekdk in https://github.com/rrousselGit/freezed/issues/70#issuecomment-922116964

rrousselGit avatar Apr 02 '22 09:04 rrousselGit

@rrousselGit any workaround other than manually editing the generated file everytime? I depend heavily on freezed in my app and this is causing alot of problems since I have many conflicting imports in my freezed classes??

I am also running the generator in "watch" mode so every code change triggers rebuild of the freezed files as well.

Haidar0096 avatar Jun 06 '22 07:06 Haidar0096

I'm seeing this with code that looks basically like:

@freezed
class MyType with _$MyType {
  const factory MyType({
    required myalias.OtherType ot1,
    required List<myalias.OtherType> ot2,
  }) = _MyType;
}

The alias for ot1 is kept, but the alias for ot2, used a type parameter, is lost.

mixin _$MyType {
  n1.OtherType get ot1 => throw _privateConstructorUsedError;
  List<OtherType> get ot2 => throw _privateConstructorUsedError; // this line errors

jibbers42 avatar Oct 15 '22 05:10 jibbers42

yes,I bump into this bug too.Import a freezed class with alias will lost the alias on its CopyWith method in generated file.Its seems like the #519

LaysDragon avatar Dec 22 '22 10:12 LaysDragon

I can imagine the magnitude of addressing this bug, but this issue is only going to get worse as more and more flutter packages are developed. We have classes with names such as User and Message that produce conflicts, and another situation where two external packages also produce a conflict.

At the moment, we have to wrap our conflicts with another freezed shell model which works, but it is not a pretty or sustainable solution for freezed future use.

elango10 avatar May 03 '23 13:05 elango10

I won't work on this until we have metaprogramming.

Metaprogramming involves a complete rewrite of Freezed, and comes natively with a solution to this problem. I don't want to put a lot of effort into this when it's naturally going to disappear.

rrousselGit avatar May 03 '23 13:05 rrousselGit

So unfortunate this bug still exists...

hiroshihorie avatar Jun 26 '23 16:06 hiroshihorie

So unfortunate this bug still exists...

all we can do now is waiting the Metaprogramming

LaysDragon avatar Jun 29 '23 10:06 LaysDragon

Encountered this issue with Set<data.Company> today 😟. Does anyone have information on the roadmap for Dart Metaprogramming?

ycherniavskyi avatar Sep 16 '23 17:09 ycherniavskyi

They are discussing here,it seems like it's not going to completed right away. https://github.com/dart-lang/language/issues/1482 https://github.com/dart-lang/language/blob/main/working/macros/feature-specification.md

LaysDragon avatar Sep 16 '23 18:09 LaysDragon

Hey i'm also facing this issue.

First of all thanks for this awesome package <3.

I now it's not being fixed until metaprograming is released, but maybe it could be a warning of this issue as a current "limitation" of the package at the doc.

Caeruleo avatar Jan 29 '24 18:01 Caeruleo

Related just in case

  • #1033

myConsciousness avatar Feb 05 '24 07:02 myConsciousness