0

I want to pass a map of complex data as a parameter to a GoRoute(). However, from what I can see, the param is a String. I tried converting my Map -> String, but it immediately causes all sorts of errors due to format errors: Unexpected character (at character 2).

Im most likely going about this the incorrect way.

Is it even possible to send a Map of data as a parameter in GoRouter?

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
Mark
  • 3,653
  • 10
  • 30
  • 62
  • if your `Map` can't be converted to `String`, why don't you try to fix it? is it still usable even you can pass `Map` as parameter. because your `Map` is not the correct format? – pmatatias Dec 03 '22 at 18:50

2 Answers2

0

It can be done, but JsonEncode and then JsonDecode on the other side, I have performed it incorrectly

Mark
  • 3,653
  • 10
  • 30
  • 62
0

I would suggest you to use Model and have toJson() fromJson() methods. And pass between routes as Model using extra.


Example:

Object:

class Sample {
  String attributeA;
  String attributeB;
  bool boolValue;
  Sample(
      {required this.attributeA,
      required this.attributeB,
      required this.boolValue});}

Define GoRoute as

 GoRoute(
    path: '/sample',
    name: 'sample',
    builder: (context, state) {
      Sample sample = state.extra as Sample; // -> casting is important
      return GoToScreen(object: sample);
    },
  ),

Call it as:

Sample sample = Sample(attributeA: "True",attributeB: "False",boolValue: false)
context.goNamed("sample",extra:sample );

Receive it as:

class GoToScreen extends StatelessWidget {
  Sample? object;
  GoToScreen({super.key, this.object});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Text(
        object.toString(),
        style: const TextStyle(fontSize: 24),
      )),
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88