Flutter_map: Crash when calling setState on onPositionChanged

Created on 26 Jul 2019  路  6Comments  路  Source: fleaflet/flutter_map

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.

Screen Shot 2019-07-26 at 3 43 09 PM

onPositionChanged: (option, gestures, _) {
setState(() => _mapLocation = option.center);
},

bug

Most helpful comment

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(() { ... }
}

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reidterror picture reidterror  路  3Comments

thoffart picture thoffart  路  4Comments

JonasVautherin picture JonasVautherin  路  4Comments

DanialV picture DanialV  路  6Comments

SamuelRioTz picture SamuelRioTz  路  4Comments