Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 构造省道对象的正确方法是什么?_Flutter_Oop_Dart - Fatal编程技术网

Flutter 构造省道对象的正确方法是什么?

Flutter 构造省道对象的正确方法是什么?,flutter,oop,dart,Flutter,Oop,Dart,我很难理解飞镖物体的结构。有人能解释一下什么是构造省道对象的正确方法吗 在第一个代码段中,在使用最终标记初始化变量之前调用构造函数 class _VideoDescription extends StatelessWidget { const _VideoDescription({ Key key, this.title, this.user, this.viewCount, }) : super(key: key); final String ti

我很难理解飞镖物体的结构。有人能解释一下什么是构造省道对象的正确方法吗

在第一个代码段中,在使用最终标记初始化变量之前调用构造函数

class _VideoDescription extends StatelessWidget {
  const _VideoDescription({
    Key key,
    this.title,
    this.user,
    this.viewCount,
  }) : super(key: key);

  final String title;
  final String user;
  final int viewCount;
...
}

在第二个代码段中,当我将变量initiation放在构造函数之后并给它一个final类型时,我得到了一个错误。因此,这种结构

class Category {
  String imgUrl;
  String name;

  Category(name, imgUrl) {
    this.imgUrl = imgUrl;
    this.name = name;
  }
}
另一件事,
:super(key:key)的用法是什么在文档中第一个代码段的构造函数末尾?

默认情况下,子类中的构造函数调用超类的未命名、无参数构造函数。在构造函数体的开头调用超类的构造函数。如果还使用了初始值设定项列表,它将在调用超类之前执行。总之,执行顺序如下:

  • 初始值设定项列表
  • 超类的无参数构造函数
  • 主类的无参数构造函数
  • 关于最终变量,您可以采用以下方法:

    或:

    基本上,在执行构造函数主体之前需要初始化最终变量,因为最终变量不能更改


    关于
    super
    检查以下内容:

    void main() {
      var students = Students(1, "peter");
      print(students.name);
    }
    
    class Students extends Person {
      final int id;
    
      Students(id, age)
          : id = id,
            super(age) {
        print("I'm the constructor body");
      }
    }
    
    class Person {
      final String name;
    
      Person(name) : name = name {
        print("I'm the super constructor");
      }
    }
    
    这将打印:

    I'm the super constructor
    I'm the constructor body
    peter
    
    因此,既然这里有最终变量,那么您需要创建一个初始值设定项列表来初始化这些变量,既然
    Students
    扩展了
    Person
    ,并且由于超类
    Person
    没有无参数构造函数,那么您必须调用
    super(age)
    。执行顺序将与上述相同

    void main() {
      var students = Students(1, "peter");
      print(students.name);
    }
    
    class Students extends Person {
      final int id;
    
      Students(id, age)
          : id = id,
            super(age) {
        print("I'm the constructor body");
      }
    }
    
    class Person {
      final String name;
    
      Person(name) : name = name {
        print("I'm the super constructor");
      }
    }
    
    I'm the super constructor
    I'm the constructor body
    peter