Flutter webView:如何在loadRequest body中传递参数?

问题描述 投票:0回答:1

尝试将参数

a=1&b=2
传递到 WebView URI:

Uri uri = Uri.parse('http://localhost:8080');
var body = Uint8List.fromList(utf8.encode('a=1&b=2')));

WebViewController controller = WebViewController()
       ..loadRequest(uri, body: body);
     
WebViewWidget(controller: controller);

服务器端参数

a
b
为空。

flutter webview
1个回答
0
投票

你可以通过

Uri uri = Uri.parse('localhost:8080?a=1&b=2'); 

如果 a 和 b null 你可以像下面这样传递

Uri uri = Uri.parse('localhost:8080?a=&b=');

高级

import 'dart:convert';

void main() {
  var a = '1';
  var b = '2';
  var encodedA = Uri.encodeQueryComponent(a);
  var encodedB = Uri.encodeQueryComponent(b);
  var url = 'http://localhost:8080?a=$encodedA&b=$encodedB';
  print(url); // This will print the properly encoded URL
}

检查value是否为null;通过“”空白。

但是在Flutter中,你不能这样调用localhost。

https://medium.com/@podcoder/connecting-flutter-application-to-localhost-a1022df63130

为什么 flutter 拒绝连接 localhost:8000 或 127.0.01:8000?

© www.soinside.com 2019 - 2024. All rights reserved.