Colon after Constructor in dart

后端 未结 2 1636
北海茫月
北海茫月 2020-11-29 00:30

This code is from flutter gallery and i\'m trying to understanding and adapting it. I would know what this syntax means:

class DemoItem {
  DemoItem         


        
2条回答
  •  孤城傲影
    2020-11-29 01:12

    To elaborate on other answers and to complete the syntax, it is also possible to have a real body for the constructor along with initializer code

    NonNegativePoint(this.x, this.y) : assert(x >= 0), assert(y >= 0) {
        print('I just made a NonNegativePoint: ($x, $y)');
    }
    

    ^ Here the assertions happen before the execution of the body

    Another use case is to assign values to final fields before body executes

    final String x;
    final String y;
    
    Point.fromJson(Map json) : x = json['x'], y = json['y'] {
        print('In Point.fromJson(): ($x, $y)'); 
    }
    

提交回复
热议问题