dio的使用方式有很多,我就只选出我认为最好用的api方式做下记录,把get成post就是post请求了,网络请求都用的百度的api,实际上的response没有任何意义,所以只要打印出response有值即可。
1.最简单的请求例子,网络请求是异步的所以用async await
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:io';
void main() async{
Dio dio = Dio();
final response = await dio.get("https://www.baidu.com/");
print(response);
}
2.带有参数的get请求
void main() async{
Map paras = {"wd":"帅气的阿斌"};
Dio dio = Dio();
final response = await dio.get(
"https://www.baidu.com/s",
queryParameters: paras);
print(response);
}
3.自定义请求头,可定义的请求头dart已经为我们提供了专门的类存了对应的字符,引入以下库,就能使用HttpHeaders
import 'dart:io';
一般我们请求接收到的数据是json格式,如'accept: application/json',我们就可以这样自定义请求头
void main() async{
Options options = Options(headers: {HttpHeaders.acceptHeader:"accept: application/json"});
Map paras = {"wd":"帅气的阿斌"};
Dio dio = Dio();
final response = await dio.get(
"https://www.baidu.com/s",
queryParameters: paras,
options: options);
print(response);
}
4.使用Baseoptions
BaseOptions options = BaseOptions(
method: "get",
baseUrl: "https://www.baidu.com",
queryParameters: {
"areaCode": "",
"cateId":
"1e4bbe70-82d9-48a4-b01c-c61cb6d9096c",
"wd":"帅气的阿斌"
},
headers: {
HttpHeaders.acceptHeader: "*"
});
Dio netRequest = Dio(options);
final response = await netRequest.get('/s);
print(response);
其他详细参数设置参考如下:
abstract class HttpHeaders {
static const acceptHeader = "accept";
static const acceptCharsetHeader = "accept-charset";
static const acceptEncodingHeader = "accept-encoding";
static const acceptLanguageHeader = "accept-language";
static const acceptRangesHeader = "accept-ranges";
static const ageHeader = "age";
static const allowHeader = "allow";
static const authorizationHeader = "authorization";
static const cacheControlHeader = "cache-control";
static const connectionHeader = "connection";
static const contentEncodingHeader = "content-encoding";
static const contentLanguageHeader = "content-language";
static const contentLengthHeader = "content-length";
static const contentLocationHeader = "content-location";
static const contentMD5Header = "content-md5";
static const contentRangeHeader = "content-range";
static const contentTypeHeader = "content-type";
static const dateHeader = "date";
static const etagHeader = "etag";
static const expectHeader = "expect";
static const expiresHeader = "expires";
static const fromHeader = "from";
static const hostHeader = "host";
static const ifMatchHeader = "if-match";
static const ifModifiedSinceHeader = "if-modified-since";
static const ifNoneMatchHeader = "if-none-match";
static const ifRangeHeader = "if-range";
static const ifUnmodifiedSinceHeader = "if-unmodified-since";
static const lastModifiedHeader = "last-modified";
static const locationHeader = "location";
static const maxForwardsHeader = "max-forwards";
static const pragmaHeader = "pragma";
static const proxyAuthenticateHeader = "proxy-authenticate";
static const proxyAuthorizationHeader = "proxy-authorization";
static const rangeHeader = "range";
static const refererHeader = "referer";
static const retryAfterHeader = "retry-after";
static const serverHeader = "server";
static const teHeader = "te";
static const trailerHeader = "trailer";
static const transferEncodingHeader = "transfer-encoding";
static const upgradeHeader = "upgrade";
static const userAgentHeader = "user-agent";
static const varyHeader = "vary";
static const viaHeader = "via";
static const warningHeader = "warning";
static const wwwAuthenticateHeader = "www-authenticate";
作者:帅气的阿斌
链接:https://www.jianshu.com/p/999a738b777e
来源: