How to extend a class when one of the optional argument is not a member variable and it is required?
1,099
this.length
requires member variable.@required
requiresassert
- use
:
instead of=
- make a class extended
TickerProvider
because it isabstract class
. I mademyTickerProvider
as the example.
AppTabController appTabController = new AppTabController(mLength:10, mVsync:new myTickerProvider());
class TabController extends ChangeNotifier {
int length;
TabController({
int initialIndex = 0,
@required this.length,
@required TickerProvider vSync
}) : assert(length != null),
assert(vSync != null);
} //end of TabController
class AppTabController extends TabController {
AppTabController({int mInitialIndex,
int mLength,
TickerProvider mVsync}):super(length: mLength, vSync: mVsync);
}
class myTickerProvider extends TickerProvider{
@override
Ticker createTicker(onTick) {
// TODO: implement createTicker
return null;
}
}
Author by
Rahul Lohra
Updated on December 11, 2022Comments
-
Rahul Lohra over 1 year
Here is the code. We have a
TabController
(from sdk), I am extending thisTabController
class:class TabController extends ChangeNotifier { int length; TabController({ int initialIndex = 0, @required this.length, @required TickerProvider vSync }):assert(length != null), assert(vSync != null); } //end of TabController class AppTabController extends TabController { AppTabController(int mInitialIndex, int mLength, TickerProvider mVsync):super(length: mLength, mVsync: vsync ){} }
Now this
AppTabController
's constructor is giving syntax error. Seems like I can't extendTabController class
because:vsync
is not a member variable ofTabController
- There are some assertions in
TabController
constructor itself due to which if the required args are not passed then it will crash.
These are the compile errors:
- error: The named parameter
vsync
isn't defined