有没有办法在“Flutter web”应用程序的代码中获取用于启动应用程序的 URL?

Sat*_*ani 4 dart flutter flutter-web

使用 Dart 和 Flutter Web 构建 Web 应用程序时,我们能否以某种方式获取用于在 Dart 代码中启动 Web 应用程序的 URL?我需要为一个工具构建一个 Web 应用程序,用户将在他的一台机器上本地部署该工具,然后从浏览器启动。我需要能够在 Web 应用程序中获取服务器的 IP 地址/主机名,然后进行进一步的 gRPC/REST 调用。

Gre*_*rad 6

正如评论中提到的,您可以使用类似以下未经测试的示例:

import 'dart:html' as html;

...
// Use whichever of the following you need:
final url = html.window.location.href;
final hostname = html.window.location.hostname; // you probably need this one

Run Code Online (Sandbox Code Playgroud)