Flutter中的FutureBuilder:RangeError(RangeError(index):无效值:有效值范围为空:0) [英] FutureBuilder : RangeError (RangeError (index): Invalid value: Valid value range is empty: 0) in Flutter

查看:120
本文介绍了Flutter中的FutureBuilder:RangeError(RangeError(index):无效值:有效值范围为空:0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎读过有关此错误的文章,但常见的答案是使用 FutureBuilder 或使用 isEmpty?。我尝试了两种解决方案,但仍然出现错误。

I've read almost article about this error, but common answers were 'using FutureBuilder' or 'using isEmpty?'. I've tried both solutions, but error still comes up. Thanks in advance.

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 =等待sd.repeats();

You have to check for the length of this list 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中的FutureBuilder:RangeError(RangeError(index):无效值:有效值范围为空:0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆