【发布时间】:2021-05-28 13:13:35
【问题描述】:
我在 Flutter 中学习 api 和 http 请求,我在发出 get 请求时遇到问题,因为在任何教程中他们直接将字符串 url 粘贴到 get 作为参数,但是当我将其作为字符串发布时,它显示错误:参数类型“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}.');
}
}
这是我的错误代码的图像
【问题讨论】:
-
我已经尝试过了,我必须将函数名称 main(List
arguments) 更改为 test() 并在按钮上调用测试函数或添加 initState,@Ravinderraj singh跨度>