列表颤振中的 RangeError
RangeError in List flutter
我正在使用 count 变量作为我的 List 类型 TagColumn 的索引记录。在我的代码中,有一个加号按钮。每次按下它时,它都应该检查您是否回答了上一个文本字段。如果这样做,将创建一个 new TagColumn。换句话说,一个新的文本字段。但是,当我使用 SetState() 更新我的计数时,我收到 RangeError。
我的代码:
class Practice extends StatefulWidget {
@override
_PracticeState createState() => _PracticeState();
}
class _PracticeState extends State<Practice>{
int count = 0;
@override
Widget build(BuildContext context){
List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint){
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)
),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: (){
setState(() {
if(count != 0 && ok[count].text.text.isEmpty){
}
else{
count ++;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
)
)
)
)
],
);
}),
);
}
}
错误:
RangeError (index): Invalid value: Only valid value is 0: 1
如果您需要更多上下文,这是我之前关于我的代码中早期错误的问题:
在这里,当你检查最后一个标签是否为空时,它会抛出错误,因为索引从 0 开始,所以最后一个元素将为 count-1。
此外,您每次都在创建列表,因此文本将从文本字段中删除。为避免您可以在构建方法之外创建列表并每次都添加项目。
下面的代码可以帮助您更好地理解。
int count = 0;
List<TagColumn> ok = List(); // list created out side
@override
Widget build(BuildContext context) {
// List<TagColumn> ok = List.generate(count, (int i) => new TagColumn()); // commented list
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint) {
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: () {
setState(() {
if (count != 0 &&
ok[count - 1].text.text.isEmpty) { // cout check change
} else {
ok.add(TagColumn()); // add new item on click
count++;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
))))
],
);
}),
);
}
我正在使用 count 变量作为我的 List 类型 TagColumn 的索引记录。在我的代码中,有一个加号按钮。每次按下它时,它都应该检查您是否回答了上一个文本字段。如果这样做,将创建一个 new TagColumn。换句话说,一个新的文本字段。但是,当我使用 SetState() 更新我的计数时,我收到 RangeError。
我的代码:
class Practice extends StatefulWidget {
@override
_PracticeState createState() => _PracticeState();
}
class _PracticeState extends State<Practice>{
int count = 0;
@override
Widget build(BuildContext context){
List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint){
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)
),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: (){
setState(() {
if(count != 0 && ok[count].text.text.isEmpty){
}
else{
count ++;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
)
)
)
)
],
);
}),
);
}
}
错误:
RangeError (index): Invalid value: Only valid value is 0: 1
如果您需要更多上下文,这是我之前关于我的代码中早期错误的问题:
在这里,当你检查最后一个标签是否为空时,它会抛出错误,因为索引从 0 开始,所以最后一个元素将为 count-1。
此外,您每次都在创建列表,因此文本将从文本字段中删除。为避免您可以在构建方法之外创建列表并每次都添加项目。
下面的代码可以帮助您更好地理解。
int count = 0;
List<TagColumn> ok = List(); // list created out side
@override
Widget build(BuildContext context) {
// List<TagColumn> ok = List.generate(count, (int i) => new TagColumn()); // commented list
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint) {
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: () {
setState(() {
if (count != 0 &&
ok[count - 1].text.text.isEmpty) { // cout check change
} else {
ok.add(TagColumn()); // add new item on click
count++;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
))))
],
);
}),
);
}