Freezed: [0.10.1] Visibility issues with nested data class spread across multiple files

Created on 18 Mar 2020  Â·  8Comments  Â·  Source: rrousselGit/freezed

Suppose there exist two data classes declared in two seperated files and one has another as one of its property. Then the generated code of the parent class will try to access the _$ChildCopyWIthImpl in its _$ParentCopyWithImpl, which is not visible to the parent module.

Demo code:

//main.dart
import 'package:freezed_annotation/freezed_annotation.dart';

import 'baz.dart';
part 'main.freezed.dart';

@freezed
abstract class Parent with _$Parent {
  const factory Parent(Child child) = _Parent;
}
//baz.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'baz.freezed.dart';
@freezed
abstract class Child with _$Child {
  const factory Child(String name) = _Child;
}



md5-ebebc907968f6780cb13a9bc5da32d02



//main.freezed.dart
//...
class _$ParentCopyWithImpl<$Res> implements $ParentCopyWith<$Res> {
  //...

  @override
  $ChildCopyWith<$Res> get child {
    if (_value.child == null) {
      return null;
    }
    return _$ChildCopyWithImpl<$Res>(_value.child, (value) {
    //     ^^^^^^^^^^^^^^^^^^^
    // The method '_$ChildCopyWithImpl' isn't defined for the class '_$ParentCopyWithImpl'.
      return _then(_value.copyWith(child: value));
    });
  }
}
bug

Most helpful comment

The problem is your import. Do:

import 'package:smarty_duelist/src/domain/domain.dart' show AuthFailure, $AuthFailureCopyWith;

All 8 comments

Rename _Child to AChild, this does work.

I think the problem is, _Classes are private

That's an interesting point.
I'm not quite sure how we should solve this, to be honest.

Should the generated class always be public? 🤔

Another solution would be to re-generate the implementation files if they are private.

I'm not sure if it's a good idea though.

I still have this issue on 0.10.4, when update from 0.9.2 (on 0.9.2 i haven't). Help please @rrousselGit

@Comp0te can you give me a way to reproduce the error?

https://gist.github.com/Comp0te/63d471cea00fec99b69d527723211a62 - it's source code with nested class

https://gist.github.com/Comp0te/0cfa5aed2b6312056e56a45935750728 - it's with top class

Error occurs in this place:
`abstract class $ErrorCopyWith<$Res> {
factory $ErrorCopyWith(Error value, $Res Function(Error) then) =
_$ErrorCopyWithImpl<$Res>;
$Res call({AuthFailure failure});

$AuthFailureCopyWith<$Res> get failure; // error here
}`
@rrousselGit

The problem is your import. Do:

import 'package:smarty_duelist/src/domain/domain.dart' show AuthFailure, $AuthFailureCopyWith;

I shot myself in the foot with these imports! Thank you very much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agordeev picture agordeev  Â·  4Comments

samandmoore picture samandmoore  Â·  4Comments

freezed picture freezed  Â·  4Comments

rakakhrl picture rakakhrl  Â·  6Comments

vedantrathore picture vedantrathore  Â·  6Comments