Search code examples
functionclassflutterinitialization

1 argument required when initalizing another class in my class


I want to acces a function from another class. A solution would be to initalize the second class in the class I want to access. Like this:

 class Calendarsub extends State<Calendar> with SingleTickerProviderStateMixin{

 final TableCalendar tableCalendar;

 Calendarsub(this.tableCalendar);

When I do this I can acces the functions but the app is not running because the Stateful Widget says: "1 required Argument expected but 0 found."

class Calendar extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
  return Calendarsub();  // In this bracket must be the argument
                      // But I don't know which one
   }

  }

Solution

  • You constructor needs 1 argument - TableCalendar

    So, you have to initialize it with this value:

    TableCalendar tableCalendar = TableCalendar(); //or somethimg like that
    Calendarsub calendar = Calendarsub(tableCalendar);
    

    or make this parameter optional:

    class Calendarsub extends State<Calendar> with SingleTickerProviderStateMixin{
    
     TableCalendar tableCalendar;
    
     Calendarsub({this.tableCalendar});
    

    in second case creating will be like:

    Calendarsub calendar = Calendarsub(tableCalendar: TableCalendar());