How to extend a class when one of the optional argument is not a member variable and it is required?

1,099
  1. this.length requires member variable.
  2. @required requires assert
  3. use : instead of =
  4. make a class extended TickerProvider because it is abstract class. I made myTickerProvider 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;
  }
}
Share:
1,099
Rahul Lohra
Author by

Rahul Lohra

Updated on December 11, 2022

Comments

  • Rahul Lohra
    Rahul Lohra over 1 year

    Here is the code. We have a TabController (from sdk), I am extending this TabController 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 extend TabController class because:

    1. vsync is not a member variable of TabController
    2. 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:

    1. error: The named parameter vsync isn't defined