'Loop build widget
The widget update state in build method at first.
Because Riverpod rebuild the widget when update state, the widget repeat the rebuild many times.
I ran into a similar problem when using Provider, but I solved it by making sure the values weren't the same before doing notifyListeners() method.
Problem:
class MyWidget extends StatelessWidget {
const WidgetC({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final state = Provider.of<HomePageState>(context, listen: true);
return Text(state.value);
}
}
class MyWidgetState extends ChangeNotifier {
String value = "";
void update() {
// update value from Database.
value = getValue();
notifyListeners();
}
}
build->update->notifyListeners->rebuild->update->notifyListeners->rebuild...
Solve:
class MyWidget extends StatelessWidget {
const WidgetC({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final state = Provider.of<HomePageState>(context, listen: true);
return Text(state.value);
}
}
class MyWidgetState extends ChangeNotifier {
String value = "";
void update() {
// update value from Database.
newValue = getValue();
if(value != newValue) {
notifyListeners();
}
}
}
build->update->notifyListeners->rebuild->update->finish(same value) or rebuild(diff value)
But, I can't solve this problem with Riverpod because value is so complicated object class. (I try check hasCode but I can't solve).
Plz help me.
Thank you.
Solution 1:[1]
You need to warp you update with SetState() that your wirdget rebuilds
void update() {
setState(() {
newValue = getValue();
if(value != newValue) {
notifyListeners();
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PhilNue |
