Flutter - RangeError(索引):无效值:有效值范围为空:0
Posted
技术标签:
【中文标题】Flutter - RangeError(索引):无效值:有效值范围为空:0【英文标题】:Flutter - RangeError (index): Invalid value: Valid value range is empty: 0 【发布时间】:2021-11-13 01:31:30 【问题描述】:我想构建一个如下图所示的测验应用程序
但我得到了那个错误,那是因为组值的索引。 我是颤振和编码的新手,所以请帮助我。 我不知道如何解决它。
class _ReadingPage2State extends State<ReadingPage2>
bool _isloading = false;
List<int> groupValue = [];
List<List<int>> value = [];
int questionId;
Future<Response> loadQuestion() async
final url = Network().link('/exercise/1/questions');
SharedPreferences localStorage = await SharedPreferences.getInstance();
final token = jsonDecode(localStorage.getString('token'));
http.Response response = await http.get(Uri.parse(url), headers:
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
);
if (response.statusCode == 200)
var jsonResponce = response.body;
Response res = Response.fromJson(json.decode(jsonResponce));
return res;
else
throw Exception('Failed to load Question');
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text("Set 1"),
backgroundColor: Colors.orange,
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: FutureBuilder<Response>(
future: loadQuestion(),
builder: (context, AsyncSnapshot<Response> snapshot)
if (snapshot.hasData)
List<Result> list = snapshot.data.result;
return ListView.builder(
itemCount: list.length + 1,
itemBuilder: (context, index) => index < list.length
?
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: [
Container(
child: Column(
children: <Widget>[
Text(
snapshot
.data.result[index].questionText,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w600),
),
Container(
width: 150,
child: Row(
children: <Widget>[
Radio(
value: snapshot
.data
.result[index]
.options[0]
.id,
groupValue: groupValue[index],
onChanged: (value)
setState(()
groupValue[index] = value;
print(groupValue[index]);
);
),
Text(
snapshot.data.result[index]
.options[0].option,
style: TextStyle(fontSize: 20)),
],
),
),
Container(
width: 150,
child: Row(
children: <Widget>[
Radio(
value: snapshot
.data
.result[index]
.options[1]
.id,
groupValue: groupValue[index],
onChanged: (value)
setState(()
groupValue[index] = value;
print(groupValue[index]);
);
),
Text(
snapshot.data.result[index]
.options[1].option,
style: TextStyle(fontSize: 20)),
],
),
),
SizedBox(height: 25),
],
),
),
],
)
: MaterialButton(
minWidth: double.infinity,
height: 40,
onPressed: ()
saveAnswer(questionId, groupValue[index]);
,
color: Color(0xFFFF9D00),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
child: Text('Submit'),
),
);
else if (snapshot.hasError)
debugPrint("$snapshot.error");
return Text("$snapshot.error",
style: TextStyle(fontSize: 15));
return Center(child: CircularProgressIndicator());
,
),
),
],
),
),
);
如果我将默认值设置为组值,就可以了,并且可以显示问题。但我不想为其设置默认值。
【问题讨论】:
【参考方案1】:您正在从一个列表进行迭代,而列表确实从 0 开始索引,
所以,改变
itemCount: list.length + 1,
到
itemCount: list.length,
【讨论】:
实际上我放了+1,因为我在页面末尾做了材料按钮。我尝试删除它,但仍然遇到同样的错误以上是关于Flutter - RangeError(索引):无效值:有效值范围为空:0的主要内容,如果未能解决你的问题,请参考以下文章
Flutter - RangeError(索引):无效值:有效值范围为空:0
Flutter:DioError [DioErrorType.DEFAULT]:RangeError(索引):无效值:只有有效值为0:
颤振错误:RangeError(索引):无效值:不在0..2范围内,包括:3
RangeError(索引):无效值:不在 0..6 范围内,包括:-2