How can I change the url in Flutter web?
Asked Answered
S

5

21

I need to change the content of this web page

enter image description here

for this content enter image description here

And also change the url from "localhost:49318/#/home/inicio" to "localhost:49318/#/home/other".

All of this without using the Navigator.

In a mobile app I would change the widget shown with a set state, but doing that on a web page wouldn't change the url.

Symphonia answered 18/12, 2019 at 22:39 Comment(0)
C
23

I use Fluro (a package) to control my routes, but eventually I need to change the URL without redirecting.

The solution is pretty simple

Import this:

import 'dart:html' as html;

Then in your code, use this to change the URL without affecting the current page

html.window.history.pushState(null, 'home', '#/home/other');
Cal answered 22/4, 2021 at 20:8 Comment(2)
Thanks for letting us know about this package. It looks really awesome and I am going to use it in my own project.Africanist
On pressing the back button it is not going to the last screen, for that we're required to press the browser's back button twiceGissing
T
0

The url in browser won't change unless you navigate it to specific one.
Yes, setState((){}) will work in mobile(here too) but it will change/replace the widget to the new one it won't navigate to your target.

Tanga answered 15/4, 2021 at 18:15 Comment(0)
L
0

If you use Flutter Navigator 2.0 you can use a RouteNavigationParser which handles URL parsing for you.

Lightship answered 16/12, 2021 at 14:35 Comment(0)
L
0

If you want to change the url to a html page, you can just use:

Import

import 'dart:html' as html;
// or even better: import 'package:universal_html/html.dart' as html;
final myPath = 'payment.html';
html.window.location.href = myPath;

(considering your file located at web/payment.html)

Loment answered 8/4, 2022 at 15:43 Comment(0)
W
0

How about :

 SystemNavigator.routeInformationUpdated(
      uri: "url",
    );
Womanish answered 27/7 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.