I am using Flutter with the GetX package for state management. I encountered an issue where calling the update()
method from GetX during widget building results in the following error:
Unhandled Exception: setState() or markNeedsBuild() called during build.
To prevent this error, I modified the GetX package file get_controllers.dart to defer the update()
call using WidgetsBinding.instance.addPostFrameCallback. Here’s the code snippet I added:
void update([List<Object>? ids, bool condition = true]) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!condition) {
return;
}
if (ids == null) {
refresh();
} else {
for (final id in ids) {
refreshGroup(id);
}
}
});
}
While this approach seems to work, I understand that modifying the package directly is not recommended for maintainability reasons. I want to achieve the same goal without modifying the GetX package directly. I am also unsure if my current approach is a good or bad practice.