'Undefined class error however class is imported
I am developing using Flutter/Dart. At one point I generated Code using the freezed package. The generated code is a 'part of' declarative, however it do not 'see' the package import of its parent:
import 'package:strawberry_keekz/src/domain/keekz/value_objects.dart';
part 'keekz.freezed.dart';
@freezed
abstract class Keekz implements _$Keekz {
const Keekz._();
const factory Keekz({
required UniqueId id,
required KeekzBody body,
required KeekzColor color,
}) = _Keekz;
factory Keekz.empty() => Keekz(
id: UniqueId(),
body: KeekzBody(''),
color: KeekzColor(KeekzColor.predefinedColors[0]),
);
}
and the generated code:
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target
part of 'keekz.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
/// @nodoc
class _$KeekzTearOff {
const _$KeekzTearOff();
_Keekz call(
{required UniqueId id, <--- Error: Undefined class 'UniqueId'
required KeekzBody body, // <--- Error: Undefined class 'KeekzBody'
required KeekzColor color}) { <--- Error: Undefined class 'KeekzColor'
return _Keekz(
id: id,
body: body,
color: color,
);
}
}
However the first line of the first snippet imported the necessary class. When I add this line manually to the generated code this solves the issue. Why doe it not work as the 'part of'?
Code of value_objects.dart:
import 'dart:ui';
import 'package:dartz/dartz.dart';
import 'package:strawberry_keekz/src/domain/core/failures.dart';
import 'package:strawberry_keekz/src/domain/core/value_objects.dart';
import 'package:strawberry_keekz/src/domain/core/value_transformers.dart';
import 'package:strawberry_keekz/src/domain/core/value_validators.dart';
class KeekzBody extends ValueObject<String> {
@override
final Either<ValueFailure<String>, String> value;
static const maxLength = 1000;
factory KeekzBody(String input) {
return KeekzBody._(
validateMaxStringLength(input, maxLength).flatMap(validateStringNotEmpty),
);
}
const KeekzBody._(this.value);
}
class KeekzColor extends ValueObject<Color> {
static const List<Color> predefinedColors = [
Color(0xfffafafa), // canvas
Color(0xfffa8072), // salmon
Color(0xfffedc56), // mustard
Color(0xffd0f0c0), // tea
Color(0xfffca3b7), // flamingo
Color(0xff997950), // tortilla
Color(0xfffffdd0), // cream
];
@override
final Either<ValueFailure<Color>, Color> value;
factory KeekzColor(Color input) {
return KeekzColor._(
right(makeColorOpaque(input)),
);
}
const KeekzColor._(this.value);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
