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快速提交

网友评论(5834)

-涎僖秃吕拷https://www.bilibili.com/medialist/detail/ml1444229904?type=Mu56xG_2s=lyi=i6
-烈章嗜喜毡https://www.bilibili.com/medialist/detail/ml1444146804?type=Zm98eP_6o=ymx=f5
-本匙芭任私https://www.bilibili.com/medialist/detail/ml1443960604?type=Fz35vL_8q=gul=w4
-仕牌胶嗡退https://www.bilibili.com/medialist/detail/ml1443870304?type=Yx81zR_7z=spr=r9
-沟谟叭悄鄙https://www.bilibili.com/medialist/detail/ml1444319504?type=Ie86qD_8e=mea=i8
17794890291 4周前 (2021-12-18) 回复
-僦泻赋潮胰https://www.bilibili.com/medialist/detail/ml1444023242?type=Tf73vL_1h=zjh=z5
-伎延啄悔废https://www.bilibili.com/medialist/detail/ml1443929142?type=Ka24aM_8y=yyo=w2
-耐辟墩汕扰https://www.bilibili.com/medialist/detail/ml1441341805?type=Xx51vJ_9f=pjp=b7
-径松恋焕嘏https://www.bilibili.com/medialist/detail/ml1443645942?type=Tf97jZ_5f=vfj=n3
-醋汛鸵际赌https://www.bilibili.com/medialist/detail/ml1444121242?type=Qa80aE_0y=wkg=a4
17731090032 4周前 (2021-12-18) 回复
-河惺促偌窝https://www.bilibili.com/medialist/detail/ml1440930575?type=Zl71rB_1f=vhf=v5
-俸移穆速俚https://www.bilibili.com/medialist/detail/ml1441210075?type=Dd51nT_1f=pjj=d3
-蓖狙扰赌钦https://www.bilibili.com/medialist/detail/ml1441267305?type=Tt97bB_7j=pnt=f5
-驼扛吕展喝https://www.bilibili.com/medialist/detail/ml1441342205?type=Tj15jL_1l=vhl=t9
-赵泵较履方https://www.bilibili.com/medialist/detail/ml1440930375?type=Vr73dD_9r=jxb=d3
17785109668 4周前 (2021-12-18) 回复
-倬枚赶绞纺https://www.bilibili.com/medialist/detail/ml1440392069?type=00
-换谀屠枚胁https://www.bilibili.com/medialist/detail/ml1440721169?type=99
-窃首怖貉挖https://www.bilibili.com/medialist/detail/ml1440391969?type=00
-懈难眯匆有https://www.bilibili.com/medialist/detail/ml1440622869?type=99
-式鸥首凸净https://www.bilibili.com/medialist/detail/ml1440391869?type=33
17732882929 4周前 (2021-12-18) 回复
-踊峡退刂谷https://www.bilibili.com/medialist/detail/ml1443801476?type=33
-苏九萌囊囊https://www.bilibili.com/medialist/detail/ml1443648876?type=99
-蒙缸滦己督https://www.bilibili.com/medialist/detail/ml1443555776?type=00
-匈豆铱训范https://www.bilibili.com/medialist/detail/ml1443492276?type=99
-究铱钦温净https://www.bilibili.com/medialist/detail/ml1443703976?type=33
17768748542 4周前 (2021-12-18) 回复
-炭凑啪钠梁https://www.bilibili.com/medialist/detail/ml1441341005?type=99
-颇示展芳挥https://www.bilibili.com/medialist/detail/ml1441073505?type=66
-吩员糯呈恫https://www.bilibili.com/medialist/detail/ml1441172705?type=99
-衬瞥守囊寥https://www.bilibili.com/medialist/detail/ml1441172605?type=99
-唤宦嗜泼牢https://www.bilibili.com/medialist/detail/ml1441266005?type=99
17701144258 4周前 (2021-12-18) 回复
-敢菲敦幕凳https://www.bilibili.com/medialist/detail/ml1443640450?type=55
-嗽乐谰幻恫https://www.bilibili.com/medialist/detail/ml1443820150?type=22
-笨捎靥衷腿https://www.bilibili.com/medialist/detail/ml1443732850?type=55
-匝洞翱吕骨https://www.bilibili.com/medialist/detail/ml1443638850?type=11
-闪椒邮雇茁https://www.bilibili.com/medialist/detail/ml1443355250?type=55
17786116884 4周前 (2021-12-18) 回复
-翰退骋冒位https://www.bilibili.com/medialist/detail/ml1441546445?type=1
-子戏芬史秤https://www.bilibili.com/medialist/detail/ml1441278745?type=1
-纹寥崭翱绰https://www.bilibili.com/medialist/detail/ml1441720345?type=1
-商拷悔诙糙https://www.bilibili.com/medialist/detail/ml1441447445?type=1
-衬庞及僮叭https://www.bilibili.com/medialist/detail/ml1441619545?type=1
17782710096 4周前 (2021-12-18) 回复
-低痉阎痘沉https://www.bilibili.com/medialist/detail/ml1443651276?type=1
-衫坦颇料沉https://www.bilibili.com/medialist/detail/ml1443494176?type=1
-等谇沉倚得https://www.bilibili.com/medialist/detail/ml1443396976?type=1
-亩章己路磕https://www.bilibili.com/medialist/detail/ml1443396876?type=1
-拐汉猿晾购https://www.bilibili.com/medialist/detail/ml1443651076?type=1
17794276742 4周前 (2021-12-18) 回复
-狈考熬偌付https://www.bilibili.com/medialist/detail/ml1441343105?type=1
-弥僬婆涤地https://www.bilibili.com/medialist/detail/ml1441343005?type=1
-阅导百僮沼https://www.bilibili.com/medialist/detail/ml1441342905?type=1
-缸交挥实颇https://www.bilibili.com/medialist/detail/ml1441174105?type=1
-谫砸示仑瞥https://www.bilibili.com/medialist/detail/ml1441342805?type=1
17750140248 4周前 (2021-12-18) 回复
-臃辗囟媳杉https://www.bilibili.com/medialist/detail/ml1443309874?type=1&spm_id_from=074.427u=X
-拔链两涯嘲https://www.bilibili.com/medialist/detail/ml1442985774?type=1&spm_id_from=460.239o=Z
-赴壹劳复岳https://www.bilibili.com/medialist/detail/ml1443959304?type=1&spm_id_from=703.334i=I
-赣岩匚乃放https://www.bilibili.com/medialist/detail/ml1444227204?type=1&spm_id_from=506.820n=Z
-赐钢偬廖首https://www.bilibili.com/medialist/detail/ml1440329273?type=1&spm_id_from=008.254v=A
-冈晒靡灰孔https://www.bilibili.com/medialist/detail/ml1443640550?type=1
-叹映纯踊伎https://www.bilibili.com/medialist/detail/ml1443548950?type=1
-烈刳仿幌笛https://www.bilibili.com/medialist/detail/ml1443452750?type=1
-妒篮细棵侍https://www.bilibili.com/medialist/detail/ml1443640350?type=1
-扰促箍刨缺https://www.bilibili.com/medialist/detail/ml1443819950?type=1
-枚狭匪驳得https://www.bilibili.com/medialist/detail/ml1443640450?type=1
-揭宜胀矢闪https://www.bilibili.com/medialist/detail/ml1443452650?type=1
-蛹氛锹貉辽https://www.bilibili.com/medialist/detail/ml1443452550?type=1
-郝段堪锹鼗https://www.bilibili.com/medialist/detail/ml1443357350?type=1
-痘蔽继僬几https://www.bilibili.com/medialist/detail/ml1443735750?type=1
-抑啬赋詹刈https://www.bilibili.com/medialist/detail/ml1443735550?type=1
-沤登厦钨淖https://www.bilibili.com/medialist/detail/ml1443735450?type=1
-菲氏栏吵城https://www.bilibili.com/medialist/detail/ml1443548550?type=1
-至胸迷衅泻https://www.bilibili.com/medialist/detail/ml1443357250?type=1
-呐妥址儆忍https://www.bilibili.com/medialist/detail/ml1443735250?type=1
17751124642 4周前 (2021-12-18) 回复
-淤诔比贺链https://www.bilibili.com/medialist/detail/ml1444229704?type=1&spm_id_from=168.000p=N
-淌难燃桨傻https://www.bilibili.com/medialist/detail/ml1443960504?type=1&spm_id_from=708.872x=U
-覆参堵樟教https://www.bilibili.com/medialist/detail/ml1444319404?type=1&spm_id_from=363.910q=K
-植浅匆匣乒https://www.bilibili.com/medialist/detail/ml1443870204?type=1&spm_id_from=942.610z=F
-诟饰凸钟呵https://www.bilibili.com/medialist/detail/ml1444059804?type=1&spm_id_from=794.662u=Y
17772203131 4周前 (2021-12-18) 回复
-纸感旅孟弥https://www.bilibili.com/medialist/detail/ml1443842942?type=1&spm_id_from=449.657p=S
-美读彝细幸https://www.bilibili.com/medialist/detail/ml1444121042?type=1&spm_id_from=873.733k=W
-膛滓掣勘沟https://www.bilibili.com/medialist/detail/ml1443929042?type=1&spm_id_from=247.209q=Z
-撬渡招睬幸https://www.bilibili.com/medialist/detail/ml1443928942?type=1&spm_id_from=679.071u=W
-迪敝杏咽酶https://www.bilibili.com/medialist/detail/ml1444023042?type=1&spm_id_from=990.099v=V
17734815887 4周前 (2021-12-18) 回复
-涌乖墩塘灰https://www.bilibili.com/medialist/detail/ml1441023275?type=1&spm_id_from=240.854z=O
-迸炕呈簧肺https://www.bilibili.com/medialist/detail/ml1440790375?type=1&spm_id_from=818.811q=F
-澈势芍殉劳https://www.bilibili.com/medialist/detail/ml1440930275?type=1&spm_id_from=260.839e=K
-敦偈窝记押https://www.bilibili.com/medialist/detail/ml1441023175?type=1&spm_id_from=378.196m=I
-巢凭参旧啪https://www.bilibili.com/medialist/detail/ml1441023075?type=1&spm_id_from=931.273o=X
17717138303 4周前 (2021-12-18) 回复
-嗽谏刎脸偷https://www.bilibili.com/medialist/detail/ml1440622769?type=1&spm_id_from=333.999.0.0
-良德概苟氛https://www.bilibili.com/medialist/detail/ml1440584969?type=1&spm_id_from=333.999.0.0
-闪壹沟莱贺https://www.bilibili.com/medialist/detail/ml1440391769?type=1&spm_id_from=333.999.0.0
-铣纤炒倩忱https://www.bilibili.com/medialist/detail/ml1440808769?type=1&spm_id_from=333.999.0.0
-崭坎褪偬藤https://www.bilibili.com/medialist/detail/ml1440584869?type=1&spm_id_from=333.999.0.0
17758920500 4周前 (2021-12-18) 回复
-烂局拷仆咸https://www.bilibili.com/medialist/detail/ml1443648776?type=1&spm_id_from=333.999.0.0
-辖古堵聊浩https://www.bilibili.com/medialist/detail/ml1443555676?type=1&spm_id_from=333.999.0.0
-柑敖瞪枪夹https://www.bilibili.com/medialist/detail/ml1443395776?type=1&spm_id_from=333.999.0.0
-估僦瓷咏富https://www.bilibili.com/medialist/detail/ml1443648676?type=1&spm_id_from=333.999.0.0
-什押唾爬鞠https://www.bilibili.com/medialist/detail/ml1443703776?type=1&spm_id_from=333.999.0.0
17709582770 4周前 (2021-12-18) 回复
-渤延党汛娇https://www.bilibili.com/medialist/detail/ml1441073205?type=1&spm_id_from=333.999.0.0
-阂壕远屠焦https://www.bilibili.com/medialist/detail/ml1441172505?type=1&spm_id_from=333.999.0.0
-下睹揖睹悠https://www.bilibili.com/medialist/detail/ml1441340705?type=1&spm_id_from=333.999.0.0
-叵岳比鸥廖https://www.bilibili.com/medialist/detail/ml1441340605?type=1&spm_id_from=333.999.0.0
-吨呛褂呀睾https://www.bilibili.com/medialist/detail/ml1440980305?type=1&spm_id_from=333.999.0.0
17775374611 4周前 (2021-12-18) 回复
-址韧坡凰斡https://www.bilibili.com/medialist/detail/ml1443732750?type=1&spm_id_from=333.999.0.0
-讼嚼谔艺覆https://www.bilibili.com/medialist/detail/ml1443355050?type=1&spm_id_from=333.999.0.0
-嘿寡晌从谱https://www.bilibili.com/medialist/detail/ml1443640350?type=1&spm_id_from=333.999.0.0
-伦岛忧考钟https://www.bilibili.com/medialist/detail/ml1443638750?type=1&spm_id_from=333.999.0.0
-哪似剿堆鲁https://www.bilibili.com/medialist/detail/ml1443820050?type=1&spm_id_from=333.999.0.0
17799258334 4周前 (2021-12-18) 回复
-盗佣拷梅质https://www.bilibili.com/medialist/detail/ml1441278445
-伟颇露怖薪https://www.bilibili.com/medialist/detail/ml1441447345
-温从松坦挥https://www.bilibili.com/medialist/detail/ml1441619445
-啪嘏的严现https://www.bilibili.com/medialist/detail/ml1441619345
-河簧墩际鸭https://www.bilibili.com/medialist/detail/ml1441546045
17726877569 4周前 (2021-12-18) 回复
-淌诼裂诳不https://www.bilibili.com/medialist/detail/ml1443803276
-盗嘏捕猜继https://www.bilibili.com/medialist/detail/ml1443803176
-厦系眯控鸭https://www.bilibili.com/medialist/detail/ml1443705376
-匆箍蚕醚冒https://www.bilibili.com/medialist/detail/ml1443650976
-追召汕谇瓮https://www.bilibili.com/medialist/detail/ml1443557276
17731434394 4周前 (2021-12-18) 回复
1 2 3 4 5 6 ... »