今天在写Android flutter程序的时候遇到了一个问题,可能是flutter更新后才出现的一个新问题吧。我也是第一次遇到所以想给各位老铁分享一下。
ElevatedButton(
onPressed: () async{
var url = 'http://192.168.100.118:15555/?action=newlist';
var res = await http.get(url);
String body = res.body;
var json = jsonDecode(body);
print(json);
},
child: Text("http请求")
)
flutter 网络请求http.get(url) 提示错误argument type 'String' can't be assigned to the parameter type 'Ur'。 var res = await http.get(url);提示错误如下:
解决方法是,把字符串类型的url地址通过Uri.parse()方法转换成Uri类型
ElevatedButton(
onPressed: () async{
var url = 'http://192.168.100.118:15555/?action=newlist';
var res = await http.get(Uri.parse(url));
String body = res.body;
var json = jsonDecode(body);
print(json);
print(json['items'][0]);
},
child: Text("http请求!")
)
这样就不会报错了。