运算符“+”没有为类型 'Uri' 定义

The operator '+' isn't defined for the type 'Uri'

我有一个项目,我正在使用 http 包来发出各种 http 请求。我最近更新了包,我无法将 url 链接附加到我拥有的基础 url 的末尾。这是示例代码

  var _url = Uri.parse('url here');

  var token;

  _getToken() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    token = jsonDecode(localStorage.getString('token'))['token'];
  }

  authData(data, apiUrl) async {
    try {
      var fullUrl = _url +  apiUrl;
      return await http.post(fullUrl,
          body: jsonEncode(data), headers: _setHeaders());
    } catch (e) {
      print(e);
    }
  }

这是它显示的错误

The operator '+' isn't defined for the type 'Uri'.
Try defining the operator '+'.dartundefined_operator

错误发生在 var fullUrl = _url + apiUrl;,特别是 + 运算符。在更新之前它运行良好但是我找不到它的解决方案

我使用 http.post 和字符串 url 试试这个

var _url = 'http://192.168.1.236/connect/api/v1';

以这种方式使用它,首先组合您的字符串,然后解析您的 Uri。 Uri class\object 不是可以使用 + 连接字符串的字符串。执行以下操作:

String _urlBase = 'http://192.168.1.236/connect/api/v1';

保持 getToken() 函数不变,然后使用它:

authData(data, apiUrl) async {
try {
  String _finalUrl = _urlBase + apiUrl;
  Uri _uri = Uri.parse(_finalUrl);
  return await http.post(_uri,
      body: jsonEncode(data), headers: _setHeaders());
} catch (e) {
  print(e);
}
}

这应该可以解决您的问题。