我已经读了差不多有关此错误的文章,但是常见的答案是“使用FutureBuilder
”或“使用isEmpty?
”。我已经尝试了两种解决方案,但是仍然出现错误。提前致谢。
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:dont_forget/Database/db_prRepeat.dart';
import 'package:dont_forget/Database/pr_repeat.dart';
import 'dart:core';
import 'dart:ui';
import 'dart:async';
class Progress extends StatefulWidget {
@override
_ProgressState createState() => _ProgressState();
}
class _ProgressState extends State<Progress> {
var year = DateFormat('yyyy').format(DateTime.now());
var month = DateFormat('M').format(DateTime.now());
var date = DateFormat('d').format(DateTime.now());
int ticks = 4;
void initState() {
super.initState();
loadPrRepeat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: true,
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.transparent,
bottomOpacity: 0.0,
elevation: 0.0,
title: const Text(
'진행 상황',
style: TextStyle(
fontWeight: FontWeight.w600, color: Colors.black, fontSize: 20),
),
),
body: FutureBuilder(
future: loadPrRepeat(),
builder: (context, snap) {
if (snap.data == null ||
snap.data.length == 0 ||
snap.data.isEmpty) {
return ListView(children: <Widget>[
Container(
padding: EdgeInsets.only(left: 21, right: 30),
child: Text(
"아직 진행된 학습이 없습니다. \n학습을 마치면 진행 상황을 볼 수 있습니다.\n\n예시)",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.black.withOpacity(0.5)))),
SizedBox(
height: 10,
),
GestureDetector(
onTap: () {},
child: Column(children: <Widget>[
Row(children: <Widget>[
Container(
padding:
EdgeInsets.only(left: 20, right: 115, top: 5),
child: Text("$year년 $month월 $date일",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: Colors.black))),
Container(
padding: EdgeInsets.only(top: 3),
child: Text("단어 보기",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
color: Colors.black))),
Container(
padding: EdgeInsets.only(top: 2),
child: IconButton(
icon: Icon(Icons.arrow_forward_ios,
color: Colors.black, size: 15),
onPressed: () => {}))
]),
SizedBox(height: 3),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 22, top: 3),
child: Text("복습완료",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.black))),
SizedBox(width: 10),
Container(
padding: EdgeInsets.only(top: 6, right: 1.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.yellow[600],
),
width: 30.0,
height: 30.0,
child: Text("1",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6, right: 0.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.yellow[600],
),
width: 30.0,
height: 30.0,
child: Text("2",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6, right: 0.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.yellow[600],
),
width: 30.0,
height: 30.0,
child: Text("3",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("6",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("13",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("28",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("58",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
],
)
]))
]);
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: snap.data.length.compareTo(0),
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
PrRepeat repeat = snap.data[index];
return GestureDetector(
onTap: () {},
child: Column(children: <Widget>[
Row(children: <Widget>[
Container(
padding: EdgeInsets.only(
left: 20, right: 125, top: 5),
child: RichText(
text: TextSpan(children: <TextSpan>[
TextSpan(
text: "단어 추가일\n",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black)),
TextSpan(
text:
"${repeat.year}년 ${repeat.month}월 ${repeat.date}일",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: Colors.black))
]))),
Container(
padding: EdgeInsets.only(top: 3),
child: Text("단어 보기",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
color: Colors.black))),
Container(
padding: EdgeInsets.only(top: 2),
child: IconButton(
icon: Icon(Icons.arrow_forward_ios,
color: Colors.black, size: 15),
onPressed: () => {}))
]),
SizedBox(height: 3),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 22, top: 3),
child: Text("복습완료",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.black))),
SizedBox(width: 10),
Container(
padding:
EdgeInsets.only(top: 6, right: 1.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat1') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("1",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding:
EdgeInsets.only(top: 6, right: 0.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat2') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("2",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding:
EdgeInsets.only(top: 6, right: 0.5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat3') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("3",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat4') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("6",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat5') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("13",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat6') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("28",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
SizedBox(width: 7),
Container(
padding: EdgeInsets.only(top: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color:
repeat.repeat.contains('repeat7') ==
true
? Colors.yellow[600]
: Colors.grey[200],
),
width: 30.0,
height: 30.0,
child: Text("58",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.black))),
],
),
SizedBox(height: 25)
]));
});
}
}));
}
Future<List> loadPrRepeat() async {
DBHelperPrRepeat sd = DBHelperPrRepeat();
List<PrRepeat> list = await sd.repeats();
setState(() {});
list.sort((a, b) => a.createTime.compareTo(b.createTime));
List<PrRepeat> newList = [];
if (list[0].createTime == list[1].createTime) {
newList.add(PrRepeat(
createTime: list[0].createTime,
repeat: list[0].repeat + ', ' + list[1].repeat,
year: list[0].year,
month: list[0].month,
date: list[0].date));
} else {
newList.add(list[0]);
newList.add(list[1]);
}
for (int i = 2; i < list.length; i++) {
if (list[i].createTime == newList.last.createTime) {
PrRepeat last = newList.last;
PrRepeat repeat = PrRepeat(
createTime: last.createTime,
repeat: last.repeat + ', ' + list[i].repeat,
year: last.year,
month: last.month,
date: last.date);
newList.removeLast();
newList.add(repeat);
} else {
newList.add(list[i]);
}
}
return newList;
}
}
最佳答案
您必须检查此列表List<PrRepeat> list = await sd.repeats();
的长度:
...
if(list.length == 0){
return newList;
} else if(list.length == 1){
newList.addAll(list);
return newList;
}
if (list[0].createTime == list[1].createTime) {
newList.add(PrRepeat(
createTime: list[0].createTime,
repeat: list[0].repeat + ', ' + list[1].repeat,
year: list[0].year,
month: list[0].month,
date: list[0].date));
} else {
newList.add(list[0]);
newList.add(list[1]);
}
if(list.length < 3){
return newList;
}
...
关于flutter - Flutter中的FutureBuilder:RangeError(RangeError(index):无效值:有效值范围为空:0),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63894705/