【问题标题】:http.post problem after Flutter 2 upgradeFlutter 2升级后的http.post问题
【发布时间】:2021-06-25 03:39:04
【问题描述】:

我刚刚更新到 Flutter 2。我更新了所有依赖项,包括 http。现在我遇到了以下问题:

Future<UserLogin> fetchStaff(String pUserName, String pPassword) async {
  final response = await http
      .post(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'),
          headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
          body: '{ "pUser": "$pUserName", "pPassword": "$pPassword"}')
      .timeout(Duration(seconds: kTimeOutDuration));

我收到一个错误:Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'

“参数类型'String'不能分配给参数类型'Uri'。”

$kBaseUrl = 'https://subdom.mydomain.com:443/mobile';

我需要改变什么?

【问题讨论】:

    标签: flutter http


    【解决方案1】:

    就像错误所说的那样,这是因为 post 函数需要 Uri 并且您已将 String(由 Uri.encodeFull 返回)传递给它。您需要使用Uri.parseUri 传递给它。

    final response = await http
          .post(Uri.parse('$kBaseUrl/LoginService/CheckLogin'),
    

    【讨论】:

      【解决方案2】:

      以前是用来取字符串的post方法,现在改成Uri了

      如此使用,Uri.parse 得到uri

      .post(Uri.parse(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'))
      

      【讨论】:

        【解决方案3】:

        如果你有一个字符串,比如:

        String strUri="${yourBaseUrl}/yourPath/yourFile";
        

        使用

        http.post(Uri.parse(strUri),
          headers: {...},
          body: {...},
        );
        

        足以让所有工作恢复正常。

        如果 uri 字符串作为 URI 或 URI 引用无效,您可以尝试 Uri.tryParse(strUri) 处理 null

        【讨论】:

          猜你喜欢
          • 2019-12-24
          • 2021-06-11
          • 2020-04-17
          • 2020-03-26
          • 2021-01-18
          • 1970-01-01
          • 2021-12-14
          • 2020-09-02
          • 1970-01-01
          相关资源
          最近更新 更多