Home > front end >  Getting RangeError (index): Invalid value: Valid value range is empty: 0
Getting RangeError (index): Invalid value: Valid value range is empty: 0

Time:11-20

I'm completely new to flutter like. I'm getting this error code over and over again and I don't know what i do false I hope someone can help me with my very first question I'm not long into coding like 2 months

File name : Home_screen.dart

import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:liquid_pull_to_refresh/liquid_pull_to_refresh.dart';
import 'package:mood_calendar/components/box.dart';

import '../list_data/quotes_.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;
  late final List<Content> _content = [];

  Future<void> _handleOnRefresh() async {
    fetchJson(Random).then((value) {
      setState(() {
        _content.addAll(value);
        _content.shuffle();
      });
    });
  }

  Future<List<Content>> fetchJson(jsondata) async {
    var reponse =
        await http.get(Uri.parse('https://www.jsonkeeper.com/b/4DEZ'));
    //!Api for the json text

    late List<Content> ulist = [];

    if (reponse.statusCode == 200) {
      var urjson = json.decode(reponse.body);

      for (var jsondata in urjson) {
        ulist.add(Content.fromJson(jsondata));
      }
    }
    return ulist;
  }

  @override
  void initState() {
    fetchJson(context).then((value) {
      setState(() {
        _content.addAll(value);
        _content.shuffle();
      });
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    return Scaffold(
        body: Padding(
            padding: const EdgeInsets.only(top: 8),
            child: LiquidPullToRefresh(
                backgroundColor:
                    Colors.deepPurple.shade700.withBlue(255).withOpacity(0.4),
                color: Colors.transparent,
                height: 150,
                onRefresh: _handleOnRefresh,
                showChildOpacityTransition: false,
                animSpeedFactor: 4,
                child: Padding(
                    padding: EdgeInsets.only(top: 40),
                    child: SafeArea(
                      child: ListView.builder(
                        itemBuilder: ((context, index) {
                          return (Box(
                            text: _content[index].quote.toString(),
                          ));
                        }),
                        itemCount: 10,
                      ),
                    )))));
  }
}

file name quotes.dart

import 'dart:convert';

class Content {
  Content({
    required this.id,
    required this.quote,
    required this.author,
  });

  String id;
  String quote;
  String author;

  factory Content.fromJson(Map<String, dynamic> json) => Content(
        id: json["id"],
        quote: json["quote"],
        author: json["author"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "quote": quote,
        "author": author,
      };
}

class EnumValues<T> {
  late Map<String, T> map;
  late Map<T, String> reverseMap;

  Map<T, String> get reverse {
    if (reverseMap == null) {
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    }
    return reverseMap;
  }
}

i want to set the item-count to 10 to only display 10 quotes it works but now i have this error

enter image description here

CodePudding user response:

It will take some frame to get data. Instead of providing hard-coded itemCount: 10,

provide

 ListView.builder(
   itemCount: _content.length,

To get 10 items use _content.length>=10?10:_content.length.

Also using FutureBuilder will be good choice.

  • Related