简体   繁体   English

如何制作dart中需要的超级构造函数

[英]How to make super constructor required in dart

I would like the IDE to inform me that I need to construct super constructor in inherited widget's constructor.我想 IDE 通知我需要在继承的小部件的构造函数中构造超级构造函数。 Say for example if I have abstract class like below例如,如果我有抽象 class 如下所示

abstract class BaseClass {
  final String name;

  BaseClass({
    @required String name,
  }) : this.name = name;
}

I want the child class to know that I need to implement the super constructor.我想让孩子 class 知道我需要实现超级构造函数。 The below code throws at runtime but how do I make sure that it throws in build time or IDE lets me know before build?下面的代码在运行时抛出,但我如何确保它在构建时抛出或者 IDE 在构建之前让我知道?

class ChildClass extends BaseClass{}

Step 1:步骤1:

abstract class BaseClass {
  final String name;

  BaseClass(
    String name,
  ) : this.name = name;
}

class ChildClass extends BaseClass{
  ChildClass() {}
}

You will get a warning from IDE:您将收到来自 IDE 的警告:

The superclass 'BaseClass' doesn't have a zero argument constructor.超类“BaseClass”没有零参数构造函数。

This means that you need to call the superclass's constructor in the ChildClass's constructor.这意味着您需要在 ChildClass 的构造函数中调用超类的构造函数。

Step 2:第2步:

class ChildClass extends BaseClass{
  ChildClass() : super('Shubham');
  
  get getName => name;
}

Now we can test it making an instance of ChildClass .现在我们可以创建一个ChildClass的实例来测试它。

void main() {
 var child =  ChildClass();
  print('Name : ${child.getName}');
}

Output: Output:

Shubham舒巴姆

If a derived class neglects to explicitly invoke a base class constructor, it implicitly invokes the base class's default (unnamed) constructor with zero arguments.如果派生的 class 忽略显式调用基本 class 构造函数,它会隐式调用具有零 arguments 的基类的默认(未命名)构造函数。 You therefore have several options to get a build-time warning:因此,您有多种选择来获得构建时警告:

  • Do not give the base class an unnamed constructor.不要给基础 class 一个未命名的构造函数。 Make all base class constructors named.命名所有基本 class 构造函数。
  • Make the unnamed base class constructor use one or more required positional parameters.使未命名的基础 class 构造函数使用一个或多个必需的位置参数。
  • Make the unnamed base class constructor use one or more required named parameters.使未命名的基本 class 构造函数使用一个或多个必需的命名参数。 This requires enabling Dart's null safety features and using the new required keyword instead of package:meta 's @required annotation.这需要启用 Dart 的 null 安全功能并使用新的required关键字而不是package:meta@required注释。

As stated in the doc Dart lang tour如文档Dart lang tour中所述

Constructors aren't inherited构造函数不被继承

Subclasses don't inherit constructors from their superclass.子类不从其超类继承构造函数。 A subclass that declares no constructors has only the default (no argument, no name) constructor.没有声明构造函数的子类只有默认(无参数,无名称)构造函数。

Therefore you have to call the super constructor in order to get the build time warning.因此,您必须调用超级构造函数才能获得构建时间警告。

class ChildClass extends BaseClass{
  ChildClass() : super(); // This will give you 'The parameter 'name' is required.'
}

But to get the warning when you try to instantiate it, you will need to add another required annotation anyway.但是要在尝试实例化它时得到警告,无论如何您都需要添加另一个必需的注释。

class ChildClass extends BaseClass{
  ChildClass({@required name}) : super(name: name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM