Reputation: 279
I am a begginer using flutter/Dart and I am trying to do a simple app. I want to be able to render a question and 3 different answers. When user chooses one answer, the next question popup. I get the following error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3
My code below:
class MyAppState extends State<MyApp> {
var questionIndex = 0;
void answerQuestion() {
setState(() {
questionIndex = questionIndex + 1;
});
print(questionIndex);
}
@override
Widget build(BuildContext context) {
var questions = [
"What/'s your favourite color?",
"What/'s your favourite city?",
"What/'s your favourite club?",
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("My First App"),
),
body: Column(
children: [
Text(
questions[questionIndex],
),
//........
Anyone sees the problem?
Thank you.
Upvotes: 0
Views: 386
Reputation: 26
without complete code,I can only speculate simple.
setState(() {
// replace next line by questionIndex = ++questionIndex % 3;
questionIndex = questionIndex + 1;
});
Upvotes: 1