欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter網(wǎng)絡(luò)請(qǐng)求庫DIO的基本使用

 更新時(shí)間:2021年04月08日 08:40:28   作者:楊胖胖  
這篇文章主要介紹了Flutter網(wǎng)絡(luò)請(qǐng)求庫DIO的基本使用,幫助大家更好的理解和學(xué)習(xí)使用Flutter,感興趣的朋友可以了解下

1. 導(dǎo)入dio包

目前dio庫的最新版本是3.0.1,同使用其他三方庫一樣,F(xiàn)lutter中使用dio庫同樣需要配置pubspec.yaml文件。

dependencies:
  flutter:
    sdk: flutter
  dio: ^3.0.10

2. 導(dǎo)入并創(chuàng)建實(shí)例

dio包引入成功之后就可以創(chuàng)建dio實(shí)例了,一個(gè)實(shí)例可以發(fā)起多個(gè)請(qǐng)求,APP中如果只有一個(gè)數(shù)據(jù)源的情況下就可以考慮將dio實(shí)例創(chuàng)建成單例模式,這樣可以節(jié)省系統(tǒng)資源,減少不必要的開銷。

//htpp.dart
import 'package:dio/dio.dart';
var dio = Dio();

3.基本配置

在開始使用實(shí)例之前需要對(duì)實(shí)例進(jìn)行一些基本設(shè)置,由于每個(gè)人的項(xiàng)目需求不同,我這里只寫一下我自己小項(xiàng)目的幾個(gè)簡(jiǎn)單配置:

//統(tǒng)一配置dio
  dio.options.baseUrl = "https://www.wanandroid.com";//baseUrl
  dio.options.connectTimeout = 5000;//超時(shí)時(shí)間
  dio.options.receiveTimeout = 3000;//接收數(shù)據(jù)最長(zhǎng)時(shí)間
  dio.options.responseType = ResponseType.json;//數(shù)據(jù)格式

也可以通過創(chuàng)建option的方式配置:

BaseOptions options = BaseOptions();
options.baseUrl = "https://www.wanandroid.com";
options.connectTimeout = 5000;
options.receiveTimeout = 3000;
options.responseType = ResponseType.json;
dio.options = options;

上面介紹了配置dio實(shí)例的兩種方式,并對(duì)其中的baseUrl、鏈接超時(shí)、接收數(shù)據(jù)最長(zhǎng)時(shí)長(zhǎng)、接收?qǐng)?bào)文的數(shù)據(jù)類型等幾個(gè)簡(jiǎn)單屬性做了統(tǒng)一配置。dio中還有一些其他的配置,可以參考dio的主頁github.com/flutterchin…

4.使用示例

dio實(shí)例配置完成之后如何使用呢?通過請(qǐng)求玩android首頁的banner圖來演示一下: 基本的步驟是,第一步先請(qǐng)求數(shù)據(jù),第二步把請(qǐng)求回來的json數(shù)據(jù)轉(zhuǎn)成model,第三步把model數(shù)據(jù)渲染成輪播圖:

child: FutureBuilder(
            future: dio.get("/banner/json"),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                Response response = snapshot.data;
                Map bannerMap = json.decode(response.toString());
                var banner = HomeBanner.fromJson(bannerMap);
                if (snapshot.hasError) {
                  Fluttertoast.showToast(msg: snapshot.error.toString());
                } else {
                  return _getSwiper(banner.data);
                  // Fluttertoast.showToast(msg: banner.data[0].title);
                }
              }
              return Center(
                child: CircularProgressIndicator(),
              );
            },
          ),
  //根據(jù)接口返回的數(shù)據(jù)生成輪播圖
  Swiper _getSwiper(List<Datum> data) {
    imgs.clear();
    for (var i = 0; i < data.length; i++) {
      var image = Image.network(
        data[i].imagePath,
        fit: BoxFit.cover,
      );
      imgs.add(image);
    }
    return Swiper(
      itemWidth: double.infinity,
      itemHeight: 200,
      itemCount: imgs.length,
      itemBuilder: (BuildContext context, int index) {
        return imgs[index];
      },
      autoplay: true,
      pagination: new SwiperPagination(
        builder: SwiperPagination.dots,
      ),
      control: new SwiperControl(),
    );
  }

這個(gè)示例中涉及到了JSON轉(zhuǎn)MODEL的相關(guān)知識(shí)

以上就是Flutter網(wǎng)絡(luò)請(qǐng)求庫DIO的基本使用的詳細(xì)內(nèi)容,更多關(guān)于Flutter網(wǎng)絡(luò)請(qǐng)求庫DIO的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論