31

How can I get the current URL in a Flutter Web App on which the user of the web app currently is?

For example:

app1.website.com app2.website.com

According to the result of the current URL on which the user of the web app currently is, I want to show different content.

Dharman
  • 30,962
  • 25
  • 85
  • 135
TheUniqueProgrammer
  • 605
  • 2
  • 6
  • 13

5 Answers5

41

This will get you the current url.

Uri.base

From the docs:

When running in a browser this is the current URL of the current page (from window.location.href). https://api.flutter.dev/flutter/dart-core/Uri/base.html

lights
  • 1,034
  • 1
  • 8
  • 22
  • It's not platform agnostic though. For platforms that are not web it returns the location of the application in the file system ... – Oliver Dixon Oct 10 '22 at 12:18
35

You can find it like this:

import 'dart:html';

var url = window.location.href;
coldandtired
  • 993
  • 1
  • 10
  • 13
4

I simply wanted the first part to know if I was on a main domain or subdomain to create links with.

  Uri.base.origin
Zach Gonzalez
  • 792
  • 7
  • 16
3

You can use the javascript code, like this:

import 'dart:js' as js;

FlatButton(
  child: Text("Button"),
  onPressed: () {
    print(js.context['location']['href']);
    js.context.callMethod("alert", [js.context['location']['href']]);
  },
)
xuyanjun
  • 818
  • 7
  • 8
1

Uri.base.path - to the current path on Navigator

Roger Gusmao
  • 3,788
  • 1
  • 20
  • 17