0

the question simlar on Flutter Provider setState() or markNeedsBuild() called during build

following the question , i have implemented the consume like these one

....
fetchEvents(){
    final modelBrainR = context.read<ModelBrain>();
    modelBrainR.fetchEvents();
}
...
...
...
child: Consumer<ModelBrain>(
  builder: (context, modelData, child) {

  if(modelData.action == 're_fetch'){ // here i need to trigger to load a new data again
     fetchEvents() // and i would like to change loading state to EventState.Loading // this just make sure of data // and maybe can be infinity loading but in the real case its just one time
  }
})

the conslusion of the question [mention again]

EventLoadingStatus _eventLoadingStatus = EventLoadingStatus.Loading;

...

Future<void> fetchEvents(String email) async {
  List<Event> events = await EventService().getEventsByUser(email);
  _events.clear();
  _events.addAll(events);
  _eventLoadingStatus = EventLoadingStatus.Loaded;

  notifyListeners();
}

so i still need to change _eventLoadingStatus to EventLoadingStatus.Loading; whenever modelData.action == 're_fetch' in consumer ...

or am i missing on implementation ?

its work only on outside of consumer for example i have appBar thats use InkWel / gesture detector.

// outside of consumer
AnimationClick(
            function: () {
              context.read<ModelBrain>().eventLoading();
              fetchEvents();
            },
)
class ModelBrain with ChangeNotifier {
...
  Future<void> eventLoading() async {
    _eventState = EvenState.Loading;
    notifyListeners();
  }
...
}
Yogi Arif Widodo
  • 563
  • 6
  • 22
  • Cunsumer widget is rebuild in every notifylistener called. if you want to change state, you may called it before load fetch the daata – pmatatias Jan 31 '23 at 03:59
  • can you give example with code ? because i have do it but i have checkmate by always twice doing `notifyListener` the code mention always twice or `setState() or markNeedsBuild() called during build` – Yogi Arif Widodo Jan 31 '23 at 04:05
  • where did you use this `EventLoadingStatus.Loading` ? – pmatatias Jan 31 '23 at 04:07
  • @pmatatias like on similar question , its set on default , and i need to set again while consumer trigger or has load the data and check / make sure the data `re_fetch` , so its just one time to re load again with change state loading. – Yogi Arif Widodo Jan 31 '23 at 04:16
  • 1
    ah solved by using ` Future.microtask( () => context.read().eventLoading()); ` inside of `Consumer` – Yogi Arif Widodo Jan 31 '23 at 04:23

0 Answers0