Flutter - 无法将参数类型“String”分配给参数类型“Uri”

时间:2021-03-31 18:57:47

标签: flutter dart

所以,我正在尝试在 dart 中创建一个登录/注册表单:

    // SERVER LOGIN API URL
    var url = 'https://www.mywebsite.com/apicall.php';

    // Store all data with Param Name.
    var data = {'email': email, 'password': password};

    // Starting Web API Call.
    var response = await http.post(url, body: json.encode(data));

但我收到此错误:

Error: The argument type 'String' can't be assigned to the parameter type 'Uri'.
 - 'Uri' is from 'dart:core'.
    var response = await http.post(url, body: json.encode(data));

有什么办法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:5)

http.post 需要一个 Uri 而不是 String。试试这个:

var uri = Uri.parse('https://www.mywebsite.com/apicall.php');
var response = await http.post(uri , body: json.encode(data));

答案 1 :(得分:2)

这里的错误非常明确,您使用的参数 Set-StrictMode -Version Latest $TMP = ... $HAS_SERVERS=($TMP | Select-Object Servers) if (-not $HAS_SERVERS.Servers){ echo "No servers. Abort." } else { ... } 不应该是字符串,而应该是对象 Uri。
您应该参考 sample of the http library 以了解如何轻松使用此库 :)