【问题标题】:Flutter - The argument type 'String' can't be assigned to the parameter type 'Uri' [duplicate]Flutter - 参数类型“String”不能分配给参数类型“Uri”[重复]
【发布时间】:2021-06-27 19:35:02
【问题描述】:

所以,我正在尝试在 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));

有什么办法解决吗?

【问题讨论】:

  • 正确方式:var response = await http.post(Uri.parse(url), body: json.encode(data));
  • 问:有什么办法可以解决这个问题?答:是的。例如var url = Uri.parse(''https://www.mywebsite.com/apicall.php'); http.post() 需要一个 Uri 对象。 “字符串”是一种 DIFFERENT 类型的对象。 Uri.parse() 是从“字符串”初始化“Uri”的一种方法。
  • 这能回答你的问题吗? The argument type 'String' can't be assigned to the parameter type 'Uri' BTW,很容易搜索关于特定错误消息的现有问题。

标签: flutter dart


【解决方案1】:

http.post 需要一个 Uri 而不是一个字符串。试试这个:

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

【讨论】:

    【解决方案2】:

    这里的错误非常明确,您使用的参数 url 不应该是字符串,而应该是对象 Uri。
    您应该参考sample of the http library 以了解如何轻松使用此库:)

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2021-10-17
      • 2021-06-19
      • 2021-06-30
      • 2021-06-10
      • 1970-01-01
      • 2023-04-05
      • 2021-10-02
      相关资源
      最近更新 更多