Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11190

Flutter navigation with parameters

I'm having trouble implementing a simple navigation within my app.

I have 2 routes:

  routes: <String, WidgetBuilder>{
    '/list': (BuildContext context) => wrap(ListScreen()),
    '/play': (BuildContext context) {
      final args = ModalRoute.of(context)!.settings.arguments as String;
      if (args == null) {
        throw Exception('no id');
      }
      return wrap(PrepareScreen(args));
    }
  }

And a link within the list that pushes a named route

Navigator.restorablePushNamed(context, '/play', arguments: e.id);

This works well when I click on the button, but a quick reload of the app, or a page refresh make the ModalRoute return a null value.

How do I properly implement this so the route arguments will be persisted?

Upvotes: 1

Views: 699

Answers (1)

igdmitrov
igdmitrov

Reputation: 546

I think Flutter web it is pain. I suggest my example:

import 'package:flutter/material.dart';

import 'details_page.dart';
import 'home_page.dart';

final navigatorKey = GlobalKey<NavigatorState>();

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.indigo,
      ),
      navigatorKey: navigatorKey,
      routes: <String, WidgetBuilder>{
        HomePage.routeName: (_) => const HomePage(),
      },
      onGenerateRoute: (settings) {
        print(settings);
        if (settings.name!.contains(DetailsPage.routeName)) {
          final List<String> uri = settings.name!.split('/');

          if (uri.length == 3) {
            return MaterialPageRoute(
              builder: (context) {
                return DetailsPage(pageId: uri[2]);
              },
              settings: settings,
            );
          }
        }

        return MaterialPageRoute(
          builder: (context) {
            return const HomePage();
          },
        );
      },
    );
  }
}

home_page.dart

import 'package:flutter/material.dart';

import 'details_page.dart';
import 'main.dart';

class HomePage extends StatelessWidget {
  static String routeName = '/';
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home page'),
      ),
      body: Center(
        child: Column(
          children: [
            const Text('Home page'),
            ElevatedButton(
              onPressed: () {
                navigatorKey.currentState!
                    .pushNamed("${DetailsPage.routeName}/2");
              },
              child: const Text('Go to Page 1'),
            )
          ],
        ),
      ),
    );
  }
}

details_page.dart

import 'package:flutter/material.dart';

class DetailsPage extends StatelessWidget {
  static String routeName = '/details';
  final String pageId;
  const DetailsPage({Key? key, required this.pageId}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Text('Details page'),
      ),
      body: Center(
        child: Text('Details page $pageId'),
      ),
    );
  }
}

Upvotes: 1

Related Questions