Flutter 如何消除在颤振中运行的范围错误?
我已经创建了一个字符串列表并应用了一个复选框,当复选框单击时,字符串列表将显示在下一个屏幕上,但我得到了一个范围错误。请帮忙Flutter 如何消除在颤振中运行的范围错误?,flutter,dart,range,Flutter,Dart,Range,我已经创建了一个字符串列表并应用了一个复选框,当复选框单击时,字符串列表将显示在下一个屏幕上,但我得到了一个范围错误。请帮忙 var _suggestions = <String>['this is me1','this is me2','this is me3' ]; final _saved = <String>['this is me1','this is me2','this is me3' ]; final _biggerFont = TextStyle(fon
var _suggestions = <String>['this is me1','this is me2','this is me3' ];
final _saved = <String>['this is me1','this is me2','this is me3' ];
final _biggerFont = TextStyle(fontSize: 18.0);
这是构建行函数我们也可以使用ListView.separated
ListView.separated(
padding: EdgeInsets.all(16.0),
itemBuilder: (context, index) {
return _buildRow(_suggestions[index]);
},
separatorBuilder: (_, __) => Divider(),
itemCount: _suggestions.length);
该范围错误已消失,但三个字符串值中有一个不可见。您能给我看一下\u buildRow函数吗?好的,我现在添加了\u buildRow fxn。看起来\u buildRow函数中没有问题。请在使用ListView.separated()之前检查_suggestions.length;
Widget _buildSuggestions() {
return ListView.builder(
padding: EdgeInsets.all(16.0),
itemBuilder: /*1*/ (context, i) {
if (i.isOdd) return Divider(); /*2*/
final index = i ~/ 2; /*3*/
_suggestions = <String>['this is me1','this is me2','this is me3'];
return _buildRow(_suggestions[index]);
});
}
Widget _buildRow(String pair) {
final alreadySaved = _saved.contains(pair);
return Container(
decoration: new BoxDecoration (
color: HexColor('#F2FFFF'),
border: Border.all(color: HexColor('#09B9B6')),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: ListTile(
title: Text(
pair,
style: _biggerFont,
),
trailing: Icon(
alreadySaved ? Icons.check_box : Icons.check_box_outline_blank_outlined,
color: alreadySaved ? HexColor('#09B9B6') :null,
),
onTap: (){
setState(() {
if (alreadySaved){
_saved.remove(pair);
}
else{
_saved.add(pair);
}
});
},
),
);
}
ListView.separated(
padding: EdgeInsets.all(16.0),
itemBuilder: (context, index) {
return _buildRow(_suggestions[index]);
},
separatorBuilder: (_, __) => Divider(),
itemCount: _suggestions.length);