When I Run the flutter program, this Exception has occurred.
RangeError (RangeError (index): Invalid value: Not in inclusive range 0..1: 2 how can I fix it**
Please explain what is range error and how can I solve them And why they occur and what is a possible solution to solve them.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
int _questionIndex = 0;
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
}
@override
Widget build(BuildContext context) {
var questions = [
'What\'s your favorite color?',
'What\'s your favorite animal?',
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Column(
children: [
Text(
questions[_questionIndex],
),
RaisedButton(
child: Text('Answer 1'),
onPressed: _answerQuestion,
),
RaisedButton(
child: Text('Answer 2'),
onPressed: () => print('Answer 2 chosen!'),
),
RaisedButton(
child: Text('Answer 3'),
onPressed: () {
// ...
print('Answer 3 chosen');
},
),
],
),
),
);
}
}