如何在Flutter中初始化值?它显示late初始化错误。

huangapple go评论72阅读模式
英文:

How to initialize value in flutter? It says late initialization error

问题

第一种情况

List<Student>? studentsList

在分析器中出现错误。

第二种情况

late List<Student> studentsList

它在分析器中没有出现错误,但在模拟器中出现了红屏,显示“late initialization error”。如何进行初始化?

英文:

I ran a project in flutter it says in the analyzer in the first case and in the emulator in the second case late initialization error I didn't know how to solve this problem.

First case

List&lt;Student&gt;? studentsList

It made an error in the analyzer.

Second case

late List&lt;Student&gt; studentsList

it ran and didn't make an error in the analyzer but it made a red screen in the emulator and said late initialization error.
how to initialize it?

答案1

得分: 3

Sure, here are the translated parts:

First Case

第一个情况
你说编译器 `studentsList` 可能是 `null`。因此,你可以这样访问它。
如果 (studentsList != null) {
  // 使用 studentsList 做一些操作
}
或者使用 `null aware` `?` 运算符。

```plaintext
studentsList?.add(Student());

Second Case

第二个情况
你告诉编译器,在使用 `studentsList` 之前,我会确保初始化它。所以如果你不遵守这个承诺,编译器会抛出 `LaterInitializationError`。
你可以在使用之前初始化,像这样:

```plaintext
late List<Student> studentsList = [];
或者你可以在 `initState` 中的某处初始化。
英文:

To understand it better, let me explain in simple terms.

First Case

List&lt;Student&gt;? studentsList

You are saying compiler studentsList can be potentially null. So you can access it like.

if (studentsList != null) {
  // do something with studentsList
}

or Using null aware ? operator.

studentsList?.add(Student());

Second Case

late List&lt;Student&gt; studentsList;

You are saying compiler , I will make sure I will initialize studentsList before I use it. So if you dont obey the promise compiler will throw LaterInitializationError.

You can overcome this by initializing before use like:

late List&lt;Student&gt; studentsList = [];

or you can intialize somewhere in the initState.

答案2

得分: 1

以下是翻译好的内容:

你需要在使用对象之前初始化它。

方式 1:

List<Student> studentList = [];

在这种情况下,你使用一个空列表来初始化你的 studentList 对象,而且你可以重新初始化它,因为它不是 final(最终的)。

方式 2:
如果你想要将你的列表设为 final,以便无法重新初始化它,那么可以这样做。

final List<Student> studentList = [];

在这种情况下,你可以执行添加、移除以及其他列表功能,但不能再次初始化它。

我可以告诉你,你不再需要使用 late 关键字来定义你的学生列表。

最后,如果你真的想要像这样使用 late 关键字:

late List<Student> studentList;

那么请确保你已经像下面这样初始化了你的列表,记住一件事,一旦初始化了 studentList,你就不能再次初始化它,否则会出现延迟初始化错误。

studentList = []; // 或者 studentList = otherList;
英文:

You have to initialize your object before you want to use it.

Way 1:

List&lt;Student&gt; studentList = [];

in this case you are initializing your studentList object with empty list and you can re initialize it because its not final.

Way 2:

If you want to make your list final so that you can not reinitialize it, then do like this.

final List&lt;Student&gt; studentList = [];

in this case you perform add, remove and others list functionalities without any problem but you can not re initialize it again.

I can say you do not need late keyword to define your student list anymore.

After all if you really want to use late keyword like this

late List&lt;Student&gt; studentList;

then make sure you have initialized your list like bellow and remember one thing you can not re initialize the studentList once you initialize it otherwise you will get late init error.

studentList = []; // or studentList = otherList;

答案3

得分: 0

你可以给它一个空列表和一个初始值:

List<Student>? studentsList = [];

你应该查看 Dart 基础知识:

https://dart.dev/tutorials

英文:

You can give it an empty list a initial value:

List&lt;Student&gt;? studentsList = [];

You should check out the dart basics

https://dart.dev/tutorials

答案4

得分: 0

late 关键字用于在延迟变量实例化时使用,我认为一个好的方法是创建一个初始化方法,并在第一次使用之前调用它,就像这样:

late List<Student> studentsList;

// 在代码中的某个位置初始化 studentsList,在第一次使用之前。
void initializeStudentsList() {
  studentsList = []; // 或填充它所需的数据。
}
英文:

The late keyword uses when you want to postpone variable instantiation, I think a good way is to create an initializer method and call it just before the first usage like this:

late List&lt;Student&gt; studentsList;

// Initialize studentsList somewhere in your code, just before the first usage.
void initializeStudentsList() {
  studentsList = []; // Or fill it with desired data.
}

huangapple
  • 本文由 发表于 2023年3月12日 10:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定