2

I'm trying to update the list from the server every time the widget is being built using GetXController here are my files :

controller.dart


  var newOrders = [].obs;


  getNewOrders(BuildContext context, bool notification) async {
    newOrders.clear();
    isNotification = notification;
    ApiServices().fetchNewOrderList(context).then((resp) async {
      print(resp);
      newNextURL = resp['data']['orders']['next_page_url'];
      var allItems = resp['data']['orders']['items'];
      if (allItems != null) {
        for (var item in allItems) {
          if (item['status'] == "assigned" ||
              item['status'] == "accepted" ||
              item['status'] == "arrived" ||
              item['status'] == "collected") {
            driverID = item['creator_id'].toString();
            print(item['id']);
            NewOrderModel newOrderModel = NewOrderModel(
                id: item['id'].toString(),
                distance: item['distance'],
                fee: item['fee'].toString(),
                driverID: item['creator_id'].toString(),
                status: item['status'],
                date_time: item['date_time']);
            ApiServices()
                .fetchNewOrders(context, newOrderModel.id)
                .then((secondResp) {
              print('sec $secondResp');
              newOrderModel.firebase_token =
              secondResp['data']['user_firebase_token'];
              print(newOrderModel.firebase_token);
              newOrderModel.lat = secondResp['data']['address']['lat'];
              newOrderModel.lng = secondResp['data']['address']['lng'];
              newOrderModel.address = secondResp['data']['address']['address'];

              newOrderModel.addressNote =
              secondResp['data']['address']['address_notes'];
              newOrderModel.driverID = driverID;
              newOrderModel.name = secondResp['data']['name'];
              newOrderModel.mobile = secondResp['data']['mobile'];
              newOrderModel.estimated_delivery_time =
              secondResp['data']['estimated_delivery_time'];
              newOrderModel.order_notes = secondResp['data']['order_notes'];
            });
            newOrders.add(newOrderModel);
          }
        }
      }
      isLoading.value = false;
    });
  }

and here is how am observing it in my widget :

          body: Obx(
            () => ListView(
              children: ordersC.newOrders.map((e) {
                return PreviousOrderShape(
                  isNew: true,
                  distance: e.distance,
                  theNewOrder: e,
                  fee: e.fee,
                  id: '${e.id}',
                  date_time: e.date_time,
                );
              }).toList(),
            ),
          ),

and finally here is how am calling it withing the build method :

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    ordersC.trackLocation();
    ordersC.getNewOrders(context, false); // here it is 
    return WillPopScope(
...

it works fine for the 1st build, then whenever I change the widget and go back to this widget am getting this error :

E/flutter (12435): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() or markNeedsBuild() called during build.
E/flutter (12435): This Obx widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
E/flutter (12435): The widget on which setState() or markNeedsBuild() was called was:
E/flutter (12435):   Obx
E/flutter (12435): The widget which was currently being built when the offending call was made was:
E/flutter (12435):   NewOrdersList


Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116

0 Answers0