Flutter Dart listview показывает половину данных хотя количество в разы больше? в чем моя ошибка

У меня такая проблема, данные из ответа сервера имеют количество 83 но приложение рендерит всего 39 с ошибкой RangeError (index): Invalid value: Valid value range is empty: 0, не могу понять в чем проблема, может из за того что я запуская setState в initState? но если ставлю количество на 39 хардкодом показывает норм, хотя часть данных потеряна

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
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1), () {
      loadFuture = NetworkUtils.getOrdersByUserId(id);
      NetworkUtils.getOrdersByUserId(id).then((value) {
        setState(() {
          orders = value;
        });
      });
    });
  }

  @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 {
                  //
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}

если количество 39 если количество orders.length


Ответы (1 шт):

Автор решения: MiT

Вы используете FutureBuilder, но не понимаете как он работает...

class _OrderPageState extends State<OrderPage> {
  Timer timer;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 8,
          ),
          Expanded(
            child: FutureBuilder<dynamic>(
              future: NetworkUtils.getOrdersByUserId(id),
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                if (snapshot.hasData &&
                    snapshot.connectionState != ConnectionState.waiting) {
                  return ListView.builder(
                    itemCount: snapshot.data.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 {
                  //
                }
              },
            ),
          ),
        ],
      ),
    );
  }

dynamic использовать, плохая практика...

→ Ссылка