Search code examples
flutterdartconstructorthissuper

Dart: Error: Setter not found (when defining constructor of a class)


I get the following error with this very simple constructor:

class MyHomePage extends StatefulWidget {
  MyHomePage(String title, String lang, {Key key}) {
    this.title = title;
    this.lang = lang.substring(0,2).toLowerCase();
    super(key: key);
  }


  final String title;
  final String lang;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

The error message:

Compiler message:                                                       
lib/main.dart:34:10: Error: Setter not found: 'title'.                  
    this.title = title;                                                 
         ^^^^^                                                          
lib/main.dart:35:10: Error: Setter not found: 'lang'.                   
    this.lang = lang.substring(0,2).toLowerCase();                      
         ^^^^                                                           
lib/main.dart:36:5: Error: Can't use 'super' as an expression.          
To delegate a constructor to a super constructor, put the super call as an initializer.
    super(key: key);                                                    
    ^                                                                   
lib/main.dart:34:10: Error: The setter 'title' isn't defined for the class 'MyHomePage

Why is that? I don't understand what that mean. Is there a comprehensive explanation about how to define constructors in dart? I couldn't find one on the dart.dev site.

Edit: After editing the code to the following:

class MyHomePage extends StatefulWidget {
  MyHomePage(String title, String lang, {Key key}) {
    title = title;
    lang = lang.substring(0,2).toLowerCase();
    super(key: key);
  }

  final String title;
  final String lang;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

I have the following complaints from the compiler:

Compiler message:                                                       
lib/main.dart:36:5: Error: Can't use 'super' as an expression.          
To delegate a constructor to a super constructor, put the super call as an initializer.
    super(key: key);                                                    
    ^                                                                   
lib/main.dart:39:16: Error: Final field 'title' is not initialized.     
Try to initialize the field in the declaration or in every constructor. 
  final String title;                                                   
               ^^^^^                                                    
lib/main.dart:40:16: Error: Final field 'lang' is not initialized.      
Try to initialize the field in the declaration or in every constructor. 
  final String lang;     

Solution

  • There is special syntax for initialising final variables in a constructor, answered here:

    https://stackoverflow.com/a/42864979/64435

    Using this syntax, the working code is:

    class MyHomePage extends StatefulWidget {
      MyHomePage(String title, String lang, {Key key})
          : this.title = title,
            this.lang = lang.substring(0, 2).toLowerCase(),
            super(key: key);
    
      final String title;
      final String lang;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }