New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash when calling setState on onPositionChanged #374
Comments
can you provide a short example? |
I fixed adding a little delay. It occurs when I call setState when map position (center) changes
|
What about using addPostFrameCallback? For example,
|
It seems like the |
Same happens to me. The |
This solved the issue for me. |
#719 - @override
void initState() {
super.initState();
mapController = MapController();
subscription = mapController.mapEventStream.listen(onMapEvent);
}
void onMapEvent(MapEvent mapEvent) {
if (mapEvent is MapEventMoveStart) {
// do something
} else if (mapEvent is MapEventMove) {
// do something
} else if (mapEvent is MapEventMoveEnd) {
// do something
}
} |
This happened to me to as inside Seems to me this could be a rather frequent use case. And being a dart/flutter noob it took me quite some time to drill down to the reason. I feel that this is a real issue that will hit a lot of devs. There should be a better solution than closing it. The problem is the I propose two solutions:
Here my code: onPositionChanged: (MapPosition position, bool hasGesture) async {
double? newLat = position.center?.latitude;
double? newLng = position.center?.longitude;
if (newLat != null && newLng != null) {
setState(() {
lat = newLat;
lng = newLng;
});
}
}, |
Being an idiot here...what's the reasoning for calling setState during an onPositionChanged ? Just trying to understand if there's a separate issue that needs pondering. I'm all for helping evade the issue with async or whatever, but just checking that there isn't something else amiss, even if it's documentation or whatever. |
Ok, makes sense, the nonRotatedWidgets wouldn't get updated (wondering if something could be improved there somehow).
Feels a bit clunky though and not ideal, so I can see why people just call setState (I've done similar myself in some plugins in the past and should probably rewrite) |
Yeah, or use getx's .obs to create an observable value (https://pub.dev/packages/get#reactive-state-manager). |
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);
},
The text was updated successfully, but these errors were encountered: