如何向嵌入式 Flutter Web 应用程序发送参数?

Zar*_*rko 6 flutter flutter-web

按照此处的说明,可以通过将 Flutter Web 应用程序包装到iframe元素中来将其嵌入到网页中,例如:

<iframe src="URL"></iframe>
Run Code Online (Sandbox Code Playgroud)

其中 URL 指向 Flutter Web 应用程序的 html。

现在,如果您想根据上下文预先配置 Flutter Web 应用程序,该怎么办 - 使其了解嵌入器中存在的某些外部值(iframe HTML 元素或父网页/窗口)。

也许有一个解决方案可以指示 Flutter 从 iframe 读取数据属性?例如:

<iframe src="URL" data-answer="42"></iframe>
Run Code Online (Sandbox Code Playgroud)

还有其他选择吗?

(更广泛的问题是可嵌入设备和嵌入器之间的双向通信。)

Zar*_*rko 3

设法找到一种发送参数的方法。基本上可以将 URL 查询参数放在 iframe 的 src URL 上,如下所示:

<iframe src="https://foo.bar?answer=42"></iframe>
Run Code Online (Sandbox Code Playgroud)

要在 Flutter Web 应用程序中获取查询参数,您需要:

// Use a dependency on package:universal_html/html.dart
final Map<String, String> params = Uri.parse(html.window.location.href).queryParameters;
final String answer = params['answer']; // -> and there you have the "42".
Run Code Online (Sandbox Code Playgroud)