Home > Back-end >  FutureBuilder creating infinite loop for fetching records
FutureBuilder creating infinite loop for fetching records

Time:08-08

I have created a demo project for showing orders using FutureBuilder but Its not showing order, instead its giving infinite loop , so where should I correct my code

Why it is infinite?

herewith I am sending code for my provider method to fetch orders and the code where I am using it

or is there any other better option to replace future builder..

Future<void> fetchandsetorders() async {
    print('I am fetchandsetorders method of provider');
    final url = Uri.parse(
        mylink);
    final response = await http.get(url);
    final List<OrderItem> loadedorders = [];

        final Map<String, dynamic> extradeddata = json.decode(response.body) as Map<String, dynamic>;
        extradeddata.forEach((orderid, orderdata) {
          loadedorders.add(
            OrderItem(
              id: orderid,
              products: (orderdata['products'] as List<dynamic>).map((item) {
                return CartItem(
                    id: item['id'],
                    title: item['title'],
                    quantity: item['qty'],
                    price: item['price']);
              }).toList(),
              amount: orderdata['amount'],
              date: DateTime.parse(orderdata['date']),
            ),

          );
        });

        _orders=loadedorders.reversed.toList();
        notifyListeners();


  }

class _OrderScreenState extends State<OrderScreen> {

  

  @override
  Widget build(BuildContext context) {
    final orderdata = Provider.of<Orders>(context);
    print('I am buildmethod');
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(Random().nextInt(255),
            Random().nextInt(255), Random().nextInt(255), 1),
        title: Text('List of orders: '   orderdata.ordercount.toString()),
      ),
      drawer: AppDrawer(),
      body: FutureBuilder(
        future: Provider.of<Orders>(context, listen: false).fetchandsetorders(),
        builder: (context,snapshop){
          if(snapshop.connectionState==ConnectionState.waiting)
            {
              return Center(child: CircularProgressIndicator());
            }
          else
            {
              if(snapshop.error!=null)
                {
                  return Text(snapshop.error.toString());
                }
              else
                {
                  return ListView.builder(
                      itemCount: orderdata.ordercount,
                      itemBuilder: (context, index) {
                        return OrderItemWidget(
                          order: orderdata.orders[index],
                        );
                      });
                }
            }
        },
      ),
    );
  }
}


CodePudding user response:

create a state variable for Future like

late myFuture  = Provider.of<Orders>(context, listen: false).fetchandsetorders(),

And use on

 body: FutureBuilder(
        future: myFuture,
  • Related