我正在学习API的和http请求在Flutter和我面临的问题,使一个获取请求,因为在任何教程中,他们直接粘贴字符串URL内获取作为参数,但当我张贴它作为字符串它显示错误:
无法将参数类型“String”分配给参数类型“Uri”。
有人能帮我吗?这是我的示例代码:
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
void main(List<String> arguments) async {
// This example uses the Google Books API to search for books about http.
// https://developers.google.com/books/docs/overview
var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';
// Await the http get response, then decode the json-formatted response.
var response = await http.get(url); // i am getting error here
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
var itemCount = jsonResponse['totalItems'];
print('Number of books about http: $itemCount.');
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
字符串
下面是我的代码错误的图片:
的数据
5条答案
按热度按时间r55awzrz1#
首先将http导入为http
字符串
然后使用
型
2jcobegt2#
如果还是不行,试试这个:
字符串
zf2sa74q3#
试试这个(将http添加到dependencies下的pubspec.yaml):
字符串
ogsagwnx4#
你传递字符串错误说需要一个uri,所以创建一个uri并在其中使用。
字符串
tpgth1q75#
首先,检查你的pubspec.yaml文件和HTTP版本。它应该是你可以在这里找到的实际版本:https://pub.dev/packages/http/install例如,它是:
字符串
目前
下面是我的代码,它工作得很好:
main.dart
型
我的app.dart
型
结果
型