对于我正在构建的教育应用程序,我有一个需要copyWith
方法的模块接口:
abstract class Module {
// ...
Module copyWith() => throw 'You should call the instance\'s copyWith.';
}
问题是我在模块子类的扩展名上使用了generates the copyWith方法的codegen包:
part 'audio_module.g.dart';
@immutable
@CopyWith()
class AudioModule implements Module {
/* ... */
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of audio_module;
// **************************************************************************
// CopyWithGenerator
// **************************************************************************
extension AudioModuleCopyWithExtension on AudioModule {
AudioModule copyWith(/* ... */) {
return AudioModule(/* ... */);
}
}
在我的AudioModule
类上,因此出现此错误,因为我认为它不计入扩展方法:
Missing concrete implementation of 'Module.copyWith'.
Try implementing the missing method, or make the class abstract.
如果子类(以一种或另一种方式)实现了copyWith
,为什么会发生这种情况?
谢谢
我最终使用的解决方法是改为使用functional_data code gen package,它会在其上使用copyWith方法构建一个类,然后对其进行扩展。无扩展名。
使用扩展方法,您的类AudioModule
不实现/覆盖Module
中的abstracte方法。扩展方法的工作原理类似于静态方法,因此实际上并没有将copyWith()
添加到AudioModule
。
只需查看Dart文档或Google,了解Dart扩展方法和冲突解决方法。