Update Widget on Update Status with ChangeNotifierProvider

Issue

I am new to Flutter and I am trying to make the UI refresh the page whenever there’s an action triggered by the user, e.g. on button pressed to update this order item status. I have the following codes for my widget with ChangeNotifierProvider.

    body: ChangeNotifierProvider.value(
        value: Provider.of<OrderItemModel>(context, listen: false),
        child: Consumer<OrderItemModel>(builder: (context, model, child) {...some widgets...}

I have a function named updateStatus().

  void updateStatus(orderItem, status) {
    Provider.of<OrderItemModel>(context, listen: false)
        .updateOrderStatus(orderItem, status, '');
  }

However, I couldn’t find a way to update the UI. Please help.

Solution

body: ChangeNotifierProvider(
  create: (_) => OrderItemModel(),
  child: Consumer<OrderItemModel>(builder: (context, model, child) {...some widgets...}

updateStatus needs to be defined in a Widget class further down the widget tree in order for Provider.of to work. If it isn’t, I suggest calling model.updateOrderStatus directly from the onPressed callback.

Answered By – Lee3

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.