dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Consider importing parent mappers when using inheritance from different files

Open AhmedLSayed9 opened this issue 1 year ago • 2 comments

/// File-A:
@MappableEnum()
enum SomeEnum {
  A,
  B,
}

/// File-B:
@MappableClass()
class Foo with FooMappable {
  const Foo({
    required this.someEnum,
  });

  final SomeEnum someEnum;
}

/// File-C:
@MappableClass()
class Bar extends Foo with BarMappable {
  const Bar({
    required super.someEnum,
    required this.someString,
  });

  final String someString;
}

The issue is that File-C doesn't import File-A and it won't complain as we're using super.someEnum. but the generated file will include SomeEnumMapper as InvalidType and it needs manual import.

This is hard to spot if we're ignoring analyzing generate files and will throw at runtime.

It'd be helpful if we can auto-add imports for the needed mappers of the parent class if it's not included.

AhmedLSayed9 avatar Oct 29 '23 22:10 AhmedLSayed9

Unfortunately automatically adding imports to a source file is not possible with using build_runner. I understand the problem, but I don't know if or how I could fix it.

schultek avatar Nov 12 '23 20:11 schultek

We are running in to this issue as well and it adds quite a lot of extra work, especially since it won't be caught before compiling. I'm not sure what a good generic solution could be yet, though.