gpt4 book ai didi

flutter - 学习 flutter

转载 作者:行者123 更新时间:2023-12-03 04:17:33 28 4
gpt4 key购买 nike

我是塞内加尔的阿里。我今年60岁(也许这是我真正的问题-笑脸!!!)。
我正在学习Flutter和Dart。今天,我想使用给定数据模型的列表(它的名称是Mortalite,请参见下面的代码)。
我尝试使用dartpad。我很伤心,因为我不明白为什么以下代码片段无法运行:
//https://www.dartpad.dev/

void main(){
print('Beginning');

List<Mortalite>pertes;

var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;

for (int i = 0; i < 4; i++) {
total += perteInst[i];
print(i); // prints only '0'

pertes[i] = new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total);
};
print(pertes.length); // nothing printed
print('Why?'); // nothing printed
}

class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}
非常感谢您的帮助。
科特

最佳答案

上面的代码不起作用的原因是因为您已初始化List pertes,并且要传递给pertes的元素。当您尝试将pertes的索引传递给0时,它找不到它并抛出范围错误,因为pertes还没有索引0。请查看下面的修复程序,如果您需要任何帮助,请告诉我。

void main(){
print('Beginning');

List<Mortalite>pertes=[];

var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;

for (int i = 0; i < causes.length; i++) {
total += perteInst[i]; // prints only '0'

pertes.add (new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total));
};
print(pertes.length); // nothing printed // nothing printed
}

class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}

关于flutter - 学习 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64656318/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com