Trying to make a generic route "base class", where an abstract
class defines a getter that returns the route name. Something like this:
abstract class ScreenAbstract extends StatefulWidget {
static String name;
static String get routeName => '/$name';
ScreenAbstract({Key key}) : super(key: key);
}
Then, any "screen" widget can extend this class:
class SomeScreen extends ScreenAbstract {
static final name = 'someScreen';
SomeScreen({Key key}) : super(key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
Which should then be accessible like this:
Navigator.of(context).pushNamed(SomeScreen.routeName);
Hoever, when trying that, the linter throws an error:
The getter 'routeName' isn't defined for the type 'SomeScreen'.
What am I doing wrong?
In dart there's no inheritance of static members. See Language Specification here-
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.
To tackle this, you can update your solution like this -
Abstract Parent Class -
abstract class ScreenAbstract extends StatefulWidget {
final String _name;
String get routeName => '/$_name';
ScreenAbstract(this._name, {Key key}) : super(key: key);
}
The Screen Widget that extends the Parent class -
class SomeScreen extends ScreenAbstract {
static final String name = "url";
SomeScreen({Key key}) : super(name, key: key);
@override
_SomeScreenState createState() => _SomeScreenState();
}
Then you can access it like this -
Navigator.of(context).pushNamed(SomeScreen().routeName);