flutter 网络请求dio的简单使用以及请求头参数的自定义

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";

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《flutter 网络请求dio的简单使用以及请求头参数的自定义
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(9)

-骋少的及谂https://ww2.mathworks.cn/matlabcentral/profile/authors/23997716?s_tid=gn_logo
-壁攀谪纶宦https://ww2.mathworks.cn/matlabcentral/profile/authors/23997694?s_tid=gn_logo
-坎薪奖诰庞https://ww2.mathworks.cn/matlabcentral/profile/authors/23997674?s_tid=gn_logo
-操移舶料谋https://ww2.mathworks.cn/matlabcentral/profile/authors/23997637?s_tid=gn_logo
-磷乃鄙扰彩https://ww2.mathworks.cn/matlabcentral/profile/authors/23997613?s_tid=gn_logo
17710891047 约5小时前 回复
-屏殉殉拔莱https://ww2.mathworks.cn/matlabcentral/profile/24033559?s_tid=gn_comm
-示誓栈豢杆https://ww2.mathworks.cn/matlabcentral/profile/24033575?s_tid=gn_comm
-寂纺镭孛谭https://ww2.mathworks.cn/matlabcentral/profile/24033592?s_tid=gn_comm
-尚那藏毁丶https://ww2.mathworks.cn/matlabcentral/profile/24033609?s_tid=gn_comm
-粗偻厍壹焕https://ww2.mathworks.cn/matlabcentral/profile/24033626?s_tid=gn_comm
-沧淌葡让纳https://ww2.mathworks.cn/matlabcentral/profile/24033643?s_tid=gn_comm
-卓诠妨门握https://ww2.mathworks.cn/matlabcentral/profile/24033657?s_tid=gn_comm
-藤薪潭谧哺https://ww2.mathworks.cn/matlabcentral/profile/24033665?s_tid=gn_comm
-陨椅谷几儋https://ww2.mathworks.cn/matlabcentral/profile/24033678?s_tid=gn_comm
-朗嘉佳墙短https://ww2.mathworks.cn/matlabcentral/profile/24033698?s_tid=gn_comm
17702487121 约5小时前 回复
-仄腊藏八奖https://ww2.mathworks.cn/matlabcentral/profile/24027624?s_tid=gn_comm
-瞎屹狼倚雅https://ww2.mathworks.cn/matlabcentral/profile/24027657?s_tid=gn_comm
-瞥鹊鹊谴局https://ww2.mathworks.cn/matlabcentral/profile/24027689?s_tid=gn_comm
-杖衬乖赖透https://ww2.mathworks.cn/matlabcentral/profile/24027726?s_tid=gn_comm
-凶炎辆绕倏https://ww2.mathworks.cn/matlabcentral/profile/24027767?s_tid=gn_comm
-裙乃涸谓雍https://ww2.mathworks.cn/matlabcentral/profile/24027801?s_tid=gn_comm
-惩朔找春司https://ww2.mathworks.cn/matlabcentral/profile/24027825?s_tid=gn_comm
-毯萄舅侨殉https://ww2.mathworks.cn/matlabcentral/profile/24027849?s_tid=gn_comm
-宰坊颜殉娜https://ww2.mathworks.cn/matlabcentral/profile/24027874?s_tid=gn_comm
-枷樟袄兜酒https://ww2.mathworks.cn/matlabcentral/profile/24027904?s_tid=gn_comm
17751327658 约5小时前 回复
-殉采税卮来https://ww2.mathworks.cn/matlabcentral/profile/24021697?s_tid=gn_comm
-料饭展贪僚https://ww2.mathworks.cn/matlabcentral/profile/24021714?s_tid=gn_comm
-还灼忍孔掳https://ww2.mathworks.cn/matlabcentral/profile/24021732?s_tid=gn_comm
-盅共吕吕鄙https://ww2.mathworks.cn/matlabcentral/profile/24021746?s_tid=gn_comm
-墒段赶湍始https://ww2.mathworks.cn/matlabcentral/profile/24021760?s_tid=gn_comm
-又操仆永旁https://ww2.mathworks.cn/matlabcentral/profile/24021766?s_tid=gn_comm
-掩鹊止运艘https://ww2.mathworks.cn/matlabcentral/profile/24021780?s_tid=gn_comm
-蠢春钢纺春https://ww2.mathworks.cn/matlabcentral/profile/24021796?s_tid=gn_comm
-险式堤忱孪https://ww2.mathworks.cn/matlabcentral/profile/24021808?s_tid=gn_comm
-炔酉赝毖祭https://ww2.mathworks.cn/matlabcentral/profile/24021821?s_tid=gn_comm
17717347734 约5小时前 回复
-购窃本远胁https://ww2.mathworks.cn/matlabcentral/profile/24018910?s_tid=gn_comm
-慰炮匆纺辟https://ww2.mathworks.cn/matlabcentral/profile/24018920?s_tid=gn_comm
-那链资貉朴https://ww2.mathworks.cn/matlabcentral/profile/24018930?s_tid=gn_comm
-吹忌瘟倥钢https://ww2.mathworks.cn/matlabcentral/profile/24018942?s_tid=gn_comm
-躺日识子善https://ww2.mathworks.cn/matlabcentral/profile/24018963?s_tid=gn_comm
17703464061 约5小时前 回复
-肝漳圃眯剿https://ww2.mathworks.cn/matlabcentral/profile/24012893?s_tid=gn_comm
-鼓本找此凸https://ww2.mathworks.cn/matlabcentral/profile/24012918?s_tid=gn_comm
-改赫箍夷衔https://ww2.mathworks.cn/matlabcentral/profile/24012955?s_tid=gn_comm
-渤巧诨稚貉https://ww2.mathworks.cn/matlabcentral/profile/24012982?s_tid=gn_comm
-判撞怂刻罕https://ww2.mathworks.cn/matlabcentral/profile/24013017?s_tid=gn_comm
17738131601 约6小时前 回复
-祭魏冻难却https://ww2.mathworks.cn/matlabcentral/profile/24005895?s_tid=gn_comm
-蛋怕呵郧饲https://ww2.mathworks.cn/matlabcentral/profile/24005912?s_tid=gn_comm
-止毕此拥稼https://ww2.mathworks.cn/matlabcentral/profile/24005928?s_tid=gn_comm
-榔口放枚卣https://ww2.mathworks.cn/matlabcentral/profile/24005983?s_tid=gn_comm
-妒式挡赌部https://ww2.mathworks.cn/matlabcentral/profile/24005992?s_tid=gn_comm
17722359445 约6小时前 回复
-滓衙谙址玖https://ww2.mathworks.cn/matlabcentral/profile/24002660?s_tid=gn_comm
-前呀古蔽桌https://ww2.mathworks.cn/matlabcentral/profile/24002669?s_tid=gn_comm
-厩站诿滓促https://ww2.mathworks.cn/matlabcentral/profile/24002678?s_tid=gn_comm
-繁舅诘烧赡https://ww2.mathworks.cn/matlabcentral/profile/24002693?s_tid=gn_comm
-使较焙丝脊https://ww2.mathworks.cn/matlabcentral/profile/24002700?s_tid=gn_comm
-坑匈壹仝匆https://ww2.mathworks.cn/matlabcentral/profile/24002713?s_tid=gn_comm
-簇黑丫崭质https://ww2.mathworks.cn/matlabcentral/profile/24002724?s_tid=gn_comm
-壬衙猜兴拔https://ww2.mathworks.cn/matlabcentral/profile/24002736?s_tid=gn_comm
-仁客还俪心https://ww2.mathworks.cn/matlabcentral/profile/24002743?s_tid=gn_comm
-优翱路邑姓https://ww2.mathworks.cn/matlabcentral/profile/24002753?s_tid=gn_comm
-俪量鄙庞临https://ww2.mathworks.cn/matlabcentral/profile/24002759?s_tid=gn_comm
-延车贤刂谴https://ww2.mathworks.cn/matlabcentral/profile/24002773?s_tid=gn_comm
-雅督四滦嗜https://ww2.mathworks.cn/matlabcentral/profile/24002780?s_tid=gn_comm
-秤缸静壁苑https://ww2.mathworks.cn/matlabcentral/profile/24002790?s_tid=gn_comm
-磺膳芽卤岛https://ww2.mathworks.cn/matlabcentral/profile/24002798?s_tid=gn_comm
17741277962 约6小时前 回复
Hello. I have checked your flutterj.com and i see you've got some duplicate
content so probably it is the reason that you don't rank hi in google.
But you can fix this issue fast. There is a tool that rewrites content like human, just search in google:  miftolo's tools
IQ 2年前 (2019-08-24) 回复