05-Flutter mobile base -dio commercially practical and simple introduction _ Get request

We begin this study Http request third-party Dart libraries dio, this is a people open-source project, is also the country with the most extensive library of Dart Http request.

1, dio presentation and introduction

dio is a strong Dart Http request library that supports Restful API, FormData, interceptors, request cancellation, Cookie management, file upload / download, timeout, and custom adapters.

I believe many people have been in contact with or understanding dio, but still need to take it out alone explain, because Flutter programming, the need to deal with it every day, this set of tutorials will be a lot of use dio library to interface calls and data exchange.

2, was added dio dependent

In fact, Flutter or Dart also provides us with a third-party package management tool, and npm package management front-end similar to that used frequently.

Dart package management file called pubspec.yaml, in fact, it ruled over the entire project, most of the operation is the third-party plug-ins and static files (files in the root directory of the project), if we want to introduce a third party needs to be stated in the package dependencies in. For example, we want to join dio, code is as follows:

dependencies:
    dio: ^2.0.7

After adding in the pubspec.yaml dio dependence, click on the upper right corner of the file:

After the download is successful, it will appear in the Messages in:

Note that: Now dio version 2.x version already, so do not use the 1.x version, I might use earlier, previously used version 1.x, the original project on the PC can run , but moved to another PC can not pass a parameter. The problem was to find a couple of days time, be a pit. It means that after the upgrade version 1.x version 2.x does not work, and can not carry parameters. (Authors also wish to consider when upgrading older versions of stability)

3, dio get request transmitted

Understand dio, we first started a small simple Demo, practice your hands.

案例是这样的,我们模拟去大保健(啥是大保健,别装单纯了,这也是个成人课好吗?),这时候妈妈就是我们的接口,我们需要告诉妈妈我们需要什么样的人为我们服务,然后什么样人就来到房间。用程序来解释,就是我们发送一个get请求,服务端得到请求后会根据我们发送的参数,给我一个返回一个我们需要的数据。

我在easyMock上作了一个超级简单的数据,其实只是为了作这个小案例,所以不是那么复杂,EasyMock接口地址如下。

https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian

我们看一下请求该接口会返回啥:

当然你也可以自己写一个这样的接口。

有了这样的接口后,你就可以在Flutter里访问这个请求了。不过你需要在使用的文件最上方用import引入dio.dart文件才可以。

import 'package:dio/dio.dart';

然后写一个基本get请求方法,我们暂时命名为getHttp(),方法中我们用了异步的方法,因为这样会防止后面的程序堵塞,具体代码如下:

void getHttp()async{
    try{
      Response response;
      var data={'name':'技术胖'};
      response = await Dio().get(
        "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian?name=大胸美女",
        //  queryParameters:data
      );
      return print(response);
    }catch(e){
      return print(e);
    }
  }

为了大家学习方便,我给出整个页面的代码,这样更有助于大家学习,所有代码如下:

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    getHttp();
    return new Scaffold(
      body: Center(
        child: Text("商城首页"),
//        child: Text("商城首页"),
      ),
    );
  }


  void getHttp() async{
    try{
      Response response;
      var data={'name':'技术胖'};
      response = await Dio().get(
        "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian?name=大胸美女",
      );
      return print(response);
    }catch(e){
      return print(e);
    }
  }

}

启动一下项目,控制台打印信息如下:

目前我们还只能显示在终端里,这太反人类了,下节课我们就来终止这个,让界面和我们进行同步。

4、总结

本节课学会的知识点:

  • 认识Dio库:dio是一个dart的 http请求通用库,目前也是大陆使用最广泛的库,国人开发,完全开源。
  • flutter的插件包管理:学了引入dio包,并简单的学习了pubspec.yaml的结构和编写注意事项。
  • get请求的编写:我们以一个充满正能量的小Demo讲述了get请求的实现,并成功的返回了结果。

Guess you like

Origin www.cnblogs.com/niceyoo/p/11028877.html