Dart:从抽象继承方法 class
Dart: Inherit method from abstract class
尝试创建通用路由“base class”,其中 abstract
class 定义 getter returns 路由名称。像这样:
abstract class ScreenAbstract extends StatefulWidget {
static String name;
static String get routeName => '/$name';
ScreenAbstract({Key key}) : super(key: key);
}
然后,任何“屏幕”小部件都可以扩展此 class:
class SomeScreen extends ScreenAbstract {
static final name = 'someScreen';
SomeScreen({Key key}) : super(key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
应该可以这样访问:
Navigator.of(context).pushNamed(SomeScreen.routeName);
然而,当尝试这样做时,linter 会抛出一个错误:
The getter 'routeName' isn't defined for the type 'SomeScreen'.
我做错了什么?
在 dart 中没有静态成员的继承。见Language Specification这里-
Inheritance of static methods has little utility in Dart. Static
methods cannot be overridden. Any required static function can be
obtained from its declaring library, and there is no need to bring it
into scope via inheritance. Experience shows that developers are
confused by the idea of inherited methods that are not instance
methods.
Of course, the entire notion of static methods is debatable, but it is
retained here because so many programmers are familiar with it. Dart
static methods may be seen as functions of the enclosing library.
要解决这个问题,您可以像这样更新您的解决方案 -
抽象父级 Class -
abstract class ScreenAbstract extends StatefulWidget {
final String _name;
String get routeName => '/$_name';
ScreenAbstract(this._name, {Key key}) : super(key: key);
}
扩展父级的屏幕小部件class -
class SomeScreen extends ScreenAbstract {
static final String name = "url";
SomeScreen({Key key}) : super(name, key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
然后就可以这样访问了-
Navigator.of(context).pushNamed(SomeScreen().routeName);
尝试创建通用路由“base class”,其中 abstract
class 定义 getter returns 路由名称。像这样:
abstract class ScreenAbstract extends StatefulWidget {
static String name;
static String get routeName => '/$name';
ScreenAbstract({Key key}) : super(key: key);
}
然后,任何“屏幕”小部件都可以扩展此 class:
class SomeScreen extends ScreenAbstract {
static final name = 'someScreen';
SomeScreen({Key key}) : super(key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
应该可以这样访问:
Navigator.of(context).pushNamed(SomeScreen.routeName);
然而,当尝试这样做时,linter 会抛出一个错误:
The getter 'routeName' isn't defined for the type 'SomeScreen'.
我做错了什么?
在 dart 中没有静态成员的继承。见Language Specification这里-
Inheritance of static methods has little utility in Dart. Static methods cannot be overridden. Any required static function can be obtained from its declaring library, and there is no need to bring it into scope via inheritance. Experience shows that developers are confused by the idea of inherited methods that are not instance methods.
Of course, the entire notion of static methods is debatable, but it is retained here because so many programmers are familiar with it. Dart static methods may be seen as functions of the enclosing library.
要解决这个问题,您可以像这样更新您的解决方案 -
抽象父级 Class -
abstract class ScreenAbstract extends StatefulWidget {
final String _name;
String get routeName => '/$_name';
ScreenAbstract(this._name, {Key key}) : super(key: key);
}
扩展父级的屏幕小部件class -
class SomeScreen extends ScreenAbstract {
static final String name = "url";
SomeScreen({Key key}) : super(name, key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
然后就可以这样访问了-
Navigator.of(context).pushNamed(SomeScreen().routeName);