如何在 Flutter Web App 中获取当前 URL

The*_*mer 16 dart flutter flutter-web

如何在 Web 应用程序的用户当前所在的 Flutter Web 应用程序中获取当前 URL?

例如:

app1.website.com app2.website.com

根据Web应用程序用户当前所在的当前URL的结果,我想显示不同的内容。

col*_*red 21

你可以这样找到它:

import 'dart:html';

var url = window.location.href;
Run Code Online (Sandbox Code Playgroud)


lig*_*hts 11

这将为您提供当前的网址。

Uri.base
Run Code Online (Sandbox Code Playgroud)

从文档:

在浏览器中运行时,这是当前页面的当前 URL(来自 window.location.href)。 https://api.flutter.dev/flutter/dart-core/Uri/base.html


Zac*_*lez 7

我只是想让第一部分知道我是在主域还是子域上创建链接。

  Uri.base.origin
Run Code Online (Sandbox Code Playgroud)


xuy*_*jun 3

您可以使用 JavaScript 代码,如下所示:

import 'dart:js' as js;

FlatButton(
  child: Text("Button"),
  onPressed: () {
    print(js.context['location']['href']);
    js.context.callMethod("alert", [js.context['location']['href']]);
  },
)
Run Code Online (Sandbox Code Playgroud)