1

I am using flutter_map to show some GeoJSON polygon layers and things work nicely. I am wondering how their properties can be displayed as popup knowing that polygons can be clickable and properties can be retrieved as follows:

  void _onPolygonGeoJsonHit() {
    final hitResult = polygonGeoJsonHitNotifier.value;
    if (hitResult != null) {
      final tappedPolygon = hitResult.hitValues;
      debugPrint('Tapped polygon GeoJSON show all properties as follows: $tappedPolygon');
    }
  }

So far, these properties (in GeoJSON style) appear in the debug console after clicking on polygons:

enter image description here

How can these properties be shown in the screen as, for instance, an alert dialog or snackbar? is it possible this way or there is a smarter/better way to achieve this?

Thanks for the help,

**** UPDATE #1 ****

Following @Frank van Puffelen suggestion and some posts, I came up with:

void showSnackBar(BuildContext context) {
  final snackBar = SnackBar(content: Text('Hi, it works!'));
  _messengerKey.currentState!.showSnackBar(snackBar);
}

The question now is how can info stored in the tappedPolygon inside the onPolygonGeoJsonHit() function be passed to the showSnackBar function? From readings I understand that they are void functions and the only result from them is null, I still don't get how to use Future <String> and/or async to circumvent this (in case that's the right way to do it?).

Any help is welcomed,

**** UPDATE #2 ****

Thanks to @Frank, now the SnackBar is getting something else than the default Hi, it works! message I used as example, now it says Closure: () => void from Function '_onPolygonGeoJsonHit@28134734':.:

enter image description here

This is what I did:

  final hitResult = "some text";

  void _onPolygonGeoJsonHit() {
    setState(() {
      final hitResult = polygonGeoJsonHitNotifier.value?.hitValues.toString();
      if (result != null) {
        final origString = hitResult;
        final parts = origString.split(',');
        debugPrint('=================================');
        debugPrint('$parts');
        debugPrint('=================================');
      } else {
        debugPrint('No hit detected.');
      }
    });
  }

  void showSnackBar(BuildContext context) {
  //final snackBar = SnackBar(content: Text('Hi, it works!'));
    final snackBar = SnackBar(
      content: Text(
      '$_onPolygonGeoJsonHit',
      style: TextStyle(
        color: Colors.black,
      ),
    ),
    backgroundColor: Colors.white,
    behavior: SnackBarBehavior.floating,
    width: MediaQuery.sizeOf(context).width * 0.9,
    elevation: 0,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(10),
    ),
  );
  _messengerKey.currentState!.showSnackBar(snackBar);
}

What am I doing wrong here? I tried to call result instead of '$_onPolygonGeoJsonHit' but no luck. Also, adding result in setState(result) does not allow to call result inside the SnackBar.

11
  • Since your debugPrint statement is showing the info you're interesting in, did you consider passing that to showSnackbar: docs.flutter.dev/cookbook/design/snackbars#2-display-a-snackbar? Commented Aug 9 at 20:41
  • Thanks much for that, just testing to see what is going on with the snackbar idea, I think I have an scope issue with that output, not sure so far. Commented Aug 9 at 21:16
  • Good to hear that you're trying it. If you're having an issue making the snackbar work, please update your question to show that specific problem. Also: try to isolate the problem as I doubt the geo-data aspects and the snackbar aspects are both required to reproduce the problem. For more on this, see How to create a Minimal, Reproducible Example Commented Aug 10 at 3:22
  • I just updated this post what I think it is working, however, I still don't get how to extract the resulting info in tappedPolygon outside the void function _onPolygonGeoJsonHit() to be used by the SnackBar element, how can this be achieved? Commented Aug 10 at 17:13
  • If displaying the snackbar works, there's no need to include it in your question. If the problem is how to parse the JSON and get a specific value from it, just show that with a debugPrint (showing both what you get, and what you expected/want to get). I think it may be the "these properties" in your "How can these properties be shown in the screen as, for instance, an alert dialog or snackbar?" that is unclear. The code you shared outputs what I'd expect it to output, but what exact output were you expected from it? Commented Aug 10 at 20:58

1 Answer 1

1

After the extensive comments and updates, it's clear that the question is:

How can I use the JSON value that I have in the callback in a place where I can display (it in the UI) in a snackbar?

This is very common question for Flutter, and in your case the easiest answer is to:

  1. store the value in your _AnAppState

    class _AnAppState extends State<AnApp> {
      String jsonMessage = ""; // 👈 initial value is an empty string
      // for data query
      ...
    
  2. set that value in the callback, inside a setState

    void _onPolygonGeoJsonHit() {
      final hitResult = polygonGeoJsonHitNotifier.value;
      if (hitResult != null) {
        final tappedPolygon = hitResult.hitValues;
        // 👇 Set the new value, and tell Flutter to repain
        setState(() {
          jsonMessage = polygonGeoJsonHitNotifier.value?.hitValues.toString()
        });
        ...
    
  3. use the value in the UI build method to display the snackbar

    @override
    Widget build(BuildContext context) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(jsonMessage))); // 👈
      ...
    
Sign up to request clarification or add additional context in comments.

2 Comments

neat explanation, such a genius! that one did the trick, thanks a ton Puf! I just realized that you have a reputation of 602k! wow amazing, very well deserved! hope you are teaching these stuff at many universities, sure you can be an oustanding Professor!
Aww... thanks Gery! /me checks profile: "~30.7mpeople reached" I think I might actually have reached more folks here than I'd have done teaching uni. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.