Flutter FutureBuilder:颤振中的RangeError(RangeError(索引):无效值:有效值范围为空:0)
我几乎读过关于这个错误的文章,但常见的答案是“使用Flutter FutureBuilder:颤振中的RangeError(RangeError(索引):无效值:有效值范围为空:0),flutter,dart,flutter-futurebuilder,Flutter,Dart,Flutter Futurebuilder,我几乎读过关于这个错误的文章,但常见的答案是“使用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'; im
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;
}
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
导入“包:intl/intl.dart”;
导入“package:don_忘记/Database/db_prRepeat.dart”;
导入“包:别忘了/Database/pr_repeat.dart”;
导入“省道:核心”;
导入“dart:ui”;
导入“dart:async”;
类进度扩展了StatefulWidget{
@凌驾
_ProgressState createState()=>\u ProgressState();
}
类_ProgressState扩展了状态{
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();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
resizeToAvoidBottomPadding:false,
resizeToAvoidBottomInset:true,
背景颜色:Colors.white,
appBar:appBar(
背景颜色:颜色。透明,
底部不透明度:0.0,
标高:0.0,
标题:常量文本(
'진행 상황',
样式:TextStyle(
fontWeight:fontWeight.w600,颜色:Colors.black,fontSize:20),
),
),
正文:未来建设者(
future:loadPrRepeat(),
生成器:(上下文,捕捉){
如果(snap.data==null||
snap.data.length==0||
snap.data.isEmpty){
返回列表视图(子项:[
容器(
填充:仅限边缘设置(左:21,右:30),
子:文本(
"아직 진행된 학습이 없습니다. \N학습을 마치면 진행 상황을 볼 수 있습니다.\n\n예시)",
样式:TextStyle(
尺寸:16,
fontWeight:fontWeight.w500,
颜色:颜色。黑色。不透明度(0.5)),
大小盒子(
身高:10,
),
手势检测器(
onTap:(){},
子项:列(子项:[
世界其他地区(儿童:[
容器(
衬垫:
仅限边缘设置(左:20,右:115,顶部:5),
子项:文本(“$year년 $月월 $日期일",
样式:TextStyle(
尺寸:20,
fontWeight:fontWeight.w800,
颜色:颜色。黑色),
容器(
填充:仅限边缘设置(顶部:3),
子:文本(“단어 보기",
样式:TextStyle(
尺寸:17,
fontWeight:fontWeight.w400,
颜色:颜色。黑色),
容器(
填充:仅限边缘设置(顶部:2),
孩子:我的钮扣(
图标:图标(Icons.arrow\u forward\u ios、,
颜色:彩色。黑色,尺寸:15),
onPressed:()=>{})
]),
尺寸箱(高度:3),
划船(
儿童:[
填充物(
填充:仅限边缘设置(左:22,顶:3),
子:文本(“복습완료",
样式:TextStyle(
尺码:18,
fontWeight:fontWeight.w600,
颜色:颜色。黑色),
尺寸箱(宽度:10),
容器(
填充:仅限边缘设置(顶部:6,右侧:1.5),
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:颜色。黄色[600],
),
宽度:30.0,
身高:30.0,
子项:文本(“1”,
textAlign:textAlign.center,
样式:TextStyle(
尺寸:15,
fontWeight:fontWeight.w500,
颜色:颜色。黑色),
尺寸箱(宽度:7),
容器(
填充:仅限边缘设置(顶部:6,右侧:0.5),
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:颜色。黄色[600],
),
宽度:30.0,
身高:30.0,
子项:文本(“2”,
textAlign:textAlign.center,
样式:TextStyle(
尺寸:15,
fontWeight:fontWeight.w500,
颜色:颜色。黑色),
尺寸箱(宽度:7),
...
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;
}
...