0

I have a response from api, it have 83 objects, but my app dont render more than 39 items with error RangeError (index): Invalid value: Valid value range is empty: 0, if i replace itemCount to 39 works fine, where i have mistake? Maybe its because of I'm using setState in initState? i cant show more code cause forum block it My code Here:

  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1), () {
      loadFuture = NetworkUtils.getOrdersByUserId(id);
      NetworkUtils.getOrdersByUserId(id).then((value) {
        setState(() {
          orders = value;
        });
      });
    });
  }
class _OrderPageState extends State<OrderPage> {
  var orders = [];
  Future loadFuture;
  Timer timer;
  


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 8,
          ),
          Expanded(
            child: FutureBuilder<dynamic>(
              future: loadFuture,
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                if (snapshot.hasData &&
                    snapshot.connectionState != ConnectionState.waiting) {
                  return ListView.builder(
                    itemCount: orders.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        margin:
                            EdgeInsets.symmetric(horizontal: 8, vertical: 6),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5),
                          boxShadow: [
                            BoxShadow(
                              offset: Offset(0, 1),
                              blurRadius: 3,
                              color: Color(0xFF12153D).withOpacity(0.2),
                            ),
                          ],
                        ),
                        
                          child: Text('Here more info ')
                          
                        ),
                      );
                    },
                  );
                } else {
                  //
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}

if itemsCount = orders.length

if itemsCount = 39

1 Answers1

0

You're performing the query twice.

  1. loadFuture = NetworkUtils.getOrdersByUserId(id);
  2. NetworkUtils.getOrdersByUserId(id).then((value) {});

Change it like this:

@override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1), () {
      loadFuture = NetworkUtils.getOrdersByUserId(id);
      loadFuture.then((value) {
        setState(() {
          orders = value;
        });
      });
    });
intraector
  • 994
  • 10
  • 20
  • i got it, but my problem is still exists((( – Galym Sarsebek Nov 22 '20 at 12:47
  • it must be something wrong with `NetworkUtils.getOrdersByUserId(id)` – intraector Nov 23 '20 at 15:07
  • ```static dynamic getOrdersByUserId(id) async { var uri = host + orders + id; try { final response = await Dio().get(uri).timeout(Duration(seconds: 10), onTimeout: () { return null; }); return response.data; } catch (e) { print(e); } }``` this is my getting data function, in another page same functions works fine – Galym Sarsebek Nov 23 '20 at 18:02
  • Its correct, i have response data, all okay with it, but my builder cant render that – Galym Sarsebek Nov 26 '20 at 18:18