Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter web url navigation

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.

like image 682
Norbert Avatar asked Dec 30 '19 10:12

Norbert


People also ask

How do I manage URL on Flutter web?

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. you can also define routes with query parameters. Hope this helps!

How do I navigate to another page in Flutter web?

We can do this using Generate Routes. onGenerateRoutes is the route generator callback used when the app is navigated to a named route. Using this, we will generate routes, navigate to different page and sync the changes with the URL of the browser.


2 Answers

you must use of Navigator v2 for Web.

see more info: here and here

like image 169
Ali Bagheri Avatar answered Oct 11 '22 18:10

Ali Bagheri


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!

like image 14
Shekar Mudaliyar Avatar answered Oct 11 '22 19:10

Shekar Mudaliyar