Search code examples
flutterdartflutter-go-router

Flutter GoRouter won't let me pass int value as queryparam


Flutter GoRouter won't let me pass any value which is not String as parameter.

Error Received

The following _TypeError was thrown while handling a gesture:
type 'int' is not a subtype of type 'Iterable<dynamic>'

Page which receives the int parameter:

class ItemOne extends StatelessWidget {
  final int id;

  const ItemOne({super.key, required this.id});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Item 1'),
      ),
      body: Text('This is page for with id: $id'),
    );
  }
}

GoRouter defination

          GoRoute(
            path: '/one',
            name: 'one',
            builder: (context, state) {
              return ItemOne(
                id: state.queryParams['idGiven'] as int,
              );
            },
          ),

Button which passes the int value

            IconButton(
              onPressed: () => context.pushNamed(
                'one',
                queryParams: <String, dynamic>{
                  'idGiven': 111,
                },
              ),
              icon: const Text('Push One'),
            ),

Solution

  • Yes, indeed.

    queryParams is a map from String to String.

    If you want to pass something else, you need to convert it to a string and back.

    Or use the extra field if you are passing whole objects.