I want to display the map position (latitude and longitude) when the position changes (onPositionChanged).
When I call setState to do that the app crash.

onPositionChanged: (option, gestures, _) {
setState(() => _mapLocation = option.center);
},
can you provide a short example?
I fixed adding a little delay.
It occurs when I call setState when map position (center) changes
onPositionChanged: (option, gestures, _) {
setState((){});
},
What about using addPostFrameCallback?
For example,
@override
Widget build(BuildContext context) {
bool _building = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_building = false;
});
...
onPositionChanged: (MapPosition position, bool hasGesture, bool isUserGesture) {
if (!_building)
setState(() { ... }
}
It seems like the onPositionChanged callback is getting invoked during a build()
Same happens to me. The onPositionChanged is getting invoked with the initial map positioning I guess.
What about using addPostFrameCallback?
For example,
@override Widget build(BuildContext context) { bool _building = true; WidgetsBinding.instance.addPostFrameCallback((_) { _building = false; }); ... onPositionChanged: (MapPosition position, bool hasGesture, bool isUserGesture) { if (!_building) setState(() { ... } }
This solved the issue for me.
Most helpful comment
What about using addPostFrameCallback?
For example,