在颤振中发出 http.get() 请求的问题
Posted
技术标签:
【中文标题】在颤振中发出 http.get() 请求的问题【英文标题】:problem in making http.get() request in flutter 【发布时间】: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.');
这是我的错误代码的图像
enter image description here
【问题讨论】:
我已经尝试过了,我必须将函数名称 main(List首先,检查您的 pubspec.yaml 文件和 HTTP 版本。它应该是您可以在此处找到的实际版本:https://pub.dev/packages/http/install 例如是:
http: ^0.12.2
目前
这是我的代码,它运行良好:
main.dart
import 'package:flutter/material.dart';
import 'package:stackowerflow/my_app.dart';
void main()
runApp(MyApp());
my_app.dart
import 'dart:convert' as convert;
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class MyApp extends StatelessWidget
Future<void> stackHelp() async
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);
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.');
@override
Widget build(BuildContext context)
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter WebView '),
),
body: Container(
child: TextButton(onPressed: stackHelp, child: Text('Push me')),
),
),
);
结果
flutter: Number of books about http: 485.
【讨论】:
【参考方案2】:你传递错误说需要一个 uri 的字符串,所以创建一个 uri 并在其中使用。
var uri = new Uri.http("example.org", "/path", "q" : "http" );
【讨论】:
【参考方案3】:首先将http导入为http
import 'package:http/http.dart' as http;
然后使用解析你到 Uri 的链接
var url = Uri.parse('https://www.googleapis.com/books/v1/volumes?q=http');
http.Response response = await http.get(url);
try
if (response.statusCode == 200)
String data = response.body;
var decodedData = jsonDecode(data);
return decodedData;
else
return 'failed';
catch (e)
return 'failed';
【讨论】:
【参考方案4】:试试这个(在依赖项下添加http到pubspec.yaml):
import 'package:http/http.dart' as http;
var response = http.get(Uri.parse('https://www.google.com'));
【讨论】:
【参考方案5】:如果还是不行,试试这个:
import 'package:http/http.dart';
var response = get(Uri.parse('https://www.google.com'));
【讨论】:
以上是关于在颤振中发出 http.get() 请求的问题的主要内容,如果未能解决你的问题,请参考以下文章
在 Flutter 中每 1 分钟发出一次定时 http.get 请求的最简单方法是啥?
使用 HTTP-Basic 身份验证发出 HTTP GET 请求
从 iOS UIAutomation 发出同步 HTTP GET 请求或在 JavaScript 中调用 shell 脚本