【发布时间】:2021-09-07 03:33:58
【问题描述】:
所以,我正在尝试根据 item.lenght 将 int 列表初始化为 0,但我不断收到错误 RangeError (index): Invalid value: Valid value range is empty: 0强>
import 'package:flutter/material.dart';
class DisplayAttributes extends StatefulWidget {
const DisplayAttributes({
Key key,
//@required this.attribute,
this.data,
this.variableProducts,
Function onChanged,
}) : super(key: key);
//final Attributes attribute;
final Product data;
final List<ProductVariation> variableProducts;
@override
_DisplayAttributesState createState() => _DisplayAttributesState();
}
class _DisplayAttributesState extends State<DisplayAttributes> {
int activeOption = 0; //to replace with
List activeOptions = [];
@override
void initState() {
for (int i = 0; i >= this.widget.data.attributes.length; i++) {
activeOptions[i] = 0;
}
print(this.widget.data.attributes.length); // returns 7
print(activeOptions[0]); // returns RangeError (index): Invalid value: Valid value range is empty: 0
super.initState();
}
【问题讨论】:
-
使用
List.filled构造函数 -
谢谢,List.filled 正在工作.. 但是为什么我写的东西不起作用??
-
因为你的列表是空的所以你甚至不能做
activeOptions[0] = 0;
标签: flutter