我一直在尝试从每次迭代后都会更新的列表中构建listView,我不知道我添加的所有内容的确切问题。
照片:1. Initial Image,2. ErrorImage, 它会在itemcount完成之前跳出listview.builder 2。错误日志:
flutter: [A]
flutter: [A, I]
flutter: [I, A, R]
flutter: [A, R, I, T]
flutter: [A, I, T, R, E]
flutter: [I, R, T, E, A, L, Q, F, N, F, C, I, M, D, T, F, F, L, H, G]
flutter: [R]
flutter: [R, E]
flutter: [R, E, L]
flutter: [R, L, E, I]
flutter: [E, L, R, I, A]
flutter: [I, L, A, E, R, N]
flutter: [E, I, A, N, L, R, C]
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building:
RangeError (index): Invalid value: Not in range 0..6, inclusive: 7
When the exception was thrown, this was the stack:
#0 List.[] (dart:core-patch/growable_array.dart:149:60)
#1 _WordGameState.build.<anonymous closure>.<anonymous closure> (package:clone_4pic_1word/wordgame.dart:291:52)
#2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:15)
#3 SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1260:67)
#4 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:139:29)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 8
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (3) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 9
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (4) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 10
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (5) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 11
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (6) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 12
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (7) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 13
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (8) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 14
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (9) Exception caught by widgets library ═══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 15
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (10) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 16
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (11) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 17
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (12) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 18
════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: [R, A, I, N, L, E, C, E, E, E, F, U, T, H, L, Q, B, F, B, H]
════════ (13) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..6, inclusive: 19
════════════════════════════════════════════════════════════════════════════════════════════════════
,它再次开始工作,但是当下一个单词比上一个单词大时,即下一个单词的项数大于上一个单词时,就会出错。3. Works for smaller count,4. Gives error again here,4。错误日志:
flutter: [F]
flutter: [F, A]
flutter: [A, F, T, V, T, J, C, S, R, M, J, P, L, T, Y, I, P, T, W, D]
flutter: [U]
flutter: [U, N]
flutter: [N, U, I]
flutter: [N, U, I, L]
════════ (14) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 4
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (15) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 5
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (16) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 6
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (17) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 7
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (18) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 8
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (19) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 9
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (20) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 10
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (21) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 11
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (22) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 12
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (23) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 13
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (24) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 14
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (25) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 15
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (26) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 16
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (27) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 17
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (28) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 18
════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: [U, L, N, I, E]
════════ (29) Exception caught by widgets library ══════════════════════════════════════════════════
RangeError (index): Invalid value: Not in range 0..3, inclusive: 19
════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: [N, U, I, E, L, V]
flutter: [I, L, N, E, U, V, E]
flutter: [U, E, I, V, E, L, N, R, I, J, V, P, Z, W, O, M, U, R, G, N]
代码:
List data;
double screenWidth, screenHeight;
int value = 0;
List<String> l = [];
int lengthAnswer;
bool visiblity = false;
bool visiblityAnswer = true;
String submittext = "Submit";
bool onyesbutton = true;
@override
void initState() {
super.initState();
this.getData();
}
final answercontroller = new TextEditingController();
Future<String> getData() async {
var res =
await http.get(Uri.parse(url), headers: {"Accept": "application/json"});
this.setState(() {
var resBody = json.decode(res.body);
data = resBody["results"];
});
return "Success!";
}
_addItem() {
setState(() {
value = value + 1;
answercontroller.text = "";
onyesbutton=true;
visiblity = false;
visiblityAnswer = true;
});
}
_shuffle() {
var random = new Random();
// Go through all elements.
for (var i = l.length - 1; i > 0; i--) {
// Pick a pseudorandom number according to the list length
var n = random.nextInt(i + 1);
var temp = l[i];
l[i] = l[n];
l[n] = temp;
}
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
screenHeight = size.height;
screenWidth = size.width;
return Scaffold(
appBar: AppBar(
title: Text("4pic1game"),
),
body: ListView.builder(
itemCount: data == null ? 0 : 1,
itemBuilder: (BuildContext context, int index) {
return Container(
height: screenHeight,
width: screenWidth,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0),
child:
Visibility(
visible: visiblityAnswer,
maintainSize: false,
maintainState: true,
child: Padding(
padding: const EdgeInsets.only(
top: 15,
),
child: SizedBox(
height: 40,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: (data[value]["answer"]).toString().length,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemBuilder: (
BuildContext context,
int count,
) {
lengthAnswer =
(data[value]["answer"]).toString().length;
l.add(data[value]["answer"]
.toString()
.toUpperCase()
.substring(count, count + 1));
// ignore: unrelated_type_equality_checks
if (l.length == lengthAnswer) {
for (int i = l.length; i < 20; i++) {
_randomString(l);
}
}
print(l);
_shuffle();
return WordCardAns(
count: count,
string: data[value]["answer"]
.toString()
.toUpperCase()
.substring(count, count + 1),
);
}),
),
),
),
Visibility(
visible: visiblityAnswer,
maintainSize: false,
maintainState: true,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: TextField(
textCapitalization: TextCapitalization.characters,
controller: answercontroller,
textAlign: TextAlign.center,
decoration: InputDecoration(
fillColor: Colors.blue,
hoverColor: Colors.yellow,
focusColor: Colors.lightGreen,
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(const Radius.circular(5)),
),
),
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
letterSpacing: 5),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
elevation: 5.0,
disabledColor: Colors.grey,
onPressed:()=> onyesbutton ? showAlertDialog(context) : null,
color: Colors.green,
textColor: Colors.white,
splashColor: Colors.pink,
animationDuration: Duration(seconds: 2),
child: Text(
"Answer",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
RaisedButton(
elevation: 5.0,
onPressed: () => checkanswer(
answercontroller.text, data[value]["answer"]),
color: Colors.pink,
textColor: Colors.white,
splashColor: Colors.green,
animationDuration: Duration(seconds: 2),
child: Text(
submittext,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
],
),
),
SizedBox(
height: screenWidth / 6,
),
Visibility(
visible: visiblityAnswer,
maintainSize: false,
maintainState: true,
child: SizedBox(
height: 40,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemBuilder: (
BuildContext context,
int count,
) {
return WordCard(string: l[count]);
}),
),
),
Visibility(
visible: visiblityAnswer,
maintainState: true,
maintainSize: false,
child: SizedBox(
height: 40,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemBuilder: (
BuildContext context,
int count,
) {
// _randomString(l);
return WordCard(string: l[count + 10]);
}),
),
),
],
),
);
},
),
);
}
void checkanswer(String s, String s2) {
if (s == s2.toUpperCase() || visiblityAnswer == false) {
l.clear();
_addItem();
} else {
answercontroller.text = "";
}
}
void _randomString(List l) {
int temp;
var rand = new Random();
var list = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
];
temp = 1 + rand.nextInt(25);
l.add((list[temp]).toUpperCase());
}
showAlertDialog(BuildContext context) {
// set up the buttons
Widget cancelButton = FlatButton(
child: Text("No"),
onPressed: () {
Navigator.pop(context);
},
);
Widget continueButton = FlatButton(
child: Text("Yes"),
onPressed: () {
onyesbutton=!onyesbutton;
onyesalert();
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("AlertDialog"),
content: Text("Are you Sure?"),
actions: [
cancelButton,
continueButton,
],
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(5)),
),
);
// show the dialog
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
void onyesalert() {
{
setState(() {
visiblity = true;
visiblityAnswer = false;
submittext = "Next";
});
Navigator.pop(context);
}
}
}