15

I would like to know how can I navigate to a URL in my Flutter web app.

Currently am using the Navigator.of(context).push(MaterialPageRoute(...)); and I only get localhost:5354/#/ in the address bar.

Also I would like to know how I can I navigate to a particular URL directly by just pasting the URL into the browser's addresses bar.

1

2 Answers 2

15

You need to use named routes instead of directly using classes to routes. You can use this package named fluro https://pub.dev/packages/fluro or else you can use default navigation that flutter provides.

with fluro you can do something like this

main.dart

import '../routes/routes.dart';


void main() {
  FluroRouter.setupRouter();
// run app
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      onGenerateRoute: FluroRouter.router.generator,
    );
  }
}

routes.dart

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

class FluroRouter {
  static Router router = Router();
  static Handler _storyhandler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
          HomeView(id: params['id'][0]));
  static Handler _homehandler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
          Home());
  static void setupRouter() {
    router.define(
      '/',
      handler: _homehandler,
    );
    router.define(
      '/story/:id',
      handler: _storyhandler,
    );
  }
}

you can also define routes with query parameters.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

I tested both methods and I've only succeeded with Fluro (even if the official package page doesn't mention web comptaibility). Oddly, I get an unexpected error using the default Flutter named routes navigation on web ¯_(ツ)_/¯
@rootasjey strange that using onGenerateRoute fails to add routes to the URL but using routes adds it. :|
3

you must use of Navigator v2 for Web.

see more info: here and here

1 Comment

first link helps me to force web browser address to change on push to route. I have seen there using PageRouteBuilder(settings: RouteSettings(name: settings.name), pageBuilder:(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,) => instead of MaterialPageRoute(builder: (context) =>

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.