06-Flutter移动电商实战-dio基础_Get_Post请求和动态组件协作

06-Flutter移动电商实战-dio基础_Get_Post请求和动态组件协作

上篇文章中,我们只看到了 dio 的使用方式,但并未跟应用关联起来,所以这一篇将 dio 网络请求与应用界面结合起来,当然这也是为以后的实战作基础准备,基础打牢,我们才能飞速前进。

1、案例说明

我们还是作去“大保健”选择服务对象这个例子,不过这次我们使用按钮和动态组件来实现。具体业务逻辑是这样的:

  1. 我们制作一个文本框,用于输入需要什么样的美女为我们服务
  2. 然后点击按钮,相当于去后端请求数据
  3. 后端返回数据后,根据你的需要美女就会走进房间

一图顶千言

2、生成动态组件

可以使用stful的快捷方式,在AndroidStudio里快速生成StatefulWidget的基本结构,我们只需要改一下类的名字就可以了,就会得到如下代码.

class HomePage extends StatefulWidget {_HomePageState createState() => _HomePageState();
}class _HomePageState extends State<HomePage{@overrideWidget build(BuildContext context) {return Container(child: child,);}
}

3、加入文本框Widget

有了动态组件,咱们先把界面布局作一下。

Widget build(BuildContext context) {return Container(child: Scaffold(appBar: AppBar(title: Text('美好人间'),),body:Container(height: 1000,child: Column(children: <Widget>[TextField(controller:typeController,decoration:InputDecoration (contentPadding: EdgeInsets.all(10.0),labelText: '美女类型',helperText: '请输入你喜欢的类型'),autofocus: false,),RaisedButton(onPressed:_choiceAction,child: Text('选择完毕'),),Text(showText,overflow:TextOverflow.ellipsis,maxLines: 2,),],),) ),);}

4、Dio的get_post方法

布局完成后,可以先编写一下远程接口的调用方法,跟上节课的内容类似,不过这里返回值为一个Future,这个对象支持一个等待回掉方法then。具体代码如下:

Future getHttp(String TypeText)async{try{Response response;var data={'name':TypeText};response = await Dio().get("https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",queryParameters:data);return response.data;}catch(e){return print(e);}}

post方法如上方几乎一致,只是改变了请求方式:

 Future getHttp(String TypeText) async{try{Response response;var data={'name':TypeText};response = await Dio().post("https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/post_dabaojian",queryParameters:data);return response.data;}catch(e){return print(e);}}

为何要返回 Feature,只有返回 Feature 才能使用 then 回调。

5、得到数据后的处理

当我们写完内容后,要点击按钮,按钮会调用方法,并进行一定的判断。比如判断文本框是不是为空。然后当后端返回数据时,我们用setState方法更新了数据。

具体代码如下:

void _choiceAction(){print('开始选择你喜欢的类型............');if(typeController.text.toString()==''){showDialog(context: context,builder: (context)=>AlertDialog(title:Text('美女类型不能为空')));}else{getHttp(typeController.text.toString()).then((val){setState(() {showText=val['data']['name'].toString();});});}}

6、案例全部代码

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';class HomePage extends StatefulWidget {_HomePageState createState() => _HomePageState();
}class _HomePageState extends State<HomePage{TextEditingController typeController = TextEditingController();String showText = '欢迎你来到美好人间';@overrideWidget build(BuildContext context) {return Container(child: Scaffold(appBar: AppBar(title: Text('美好人间'),),body:Container(height: 1000,child: Column(children: <Widget>[TextField(controller:typeController,decoration:InputDecoration (contentPadding: EdgeInsets.all(10.0),labelText: '美女类型',helperText: '请输入你喜欢的类型'),autofocus: false,),RaisedButton(onPressed:_choiceAction,child: Text('选择完毕'),),Text(showText,overflow:TextOverflow.ellipsis,maxLines: 2,),],),) ),);}void _choiceAction(){print('开始选择你喜欢的类型............');if(typeController.text.toString()==''){showDialog(context: context,builder: (context)=>AlertDialog(title:Text('美女类型不能为空')));}else{getHttp(typeController.text.toString()).then((val){setState(() {showText=val['data']['name'].toString();});});}}Future getHttp(String TypeText)async{try{Response response;var data={'name':TypeText};response = await Dio().get("https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",queryParameters:data);return response.data;}catch(e){return print(e);}}
}

7、总结

通过这节课的学习,我们应该掌握如下知识点

  1. 对Flutter动态组件的深入了解
  2. Future对象的使用
  3. 改变状态和界面的setState的方法应用
  4. TextField Widget的基本使用

posted @ 2019-06-15 21:19 niceyoo 阅读(...) 评论(...) 编辑 收藏

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/414386.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Cannot merge new index 66395 into a non-jumbo instruction!,uses or overrides a deprecated API.

老项目运行没问题。一打包就报错 解决方法——添加dexOptions android {compileSdkVersion 27dexOptions{jumboMode true}

localStorage和sessionStorage的简单使用

localStorage和sessionStorage是Web提供的两种本地存储方式。 相比较cookie而言&#xff0c;localStorage和sessionStorage的存储大小很大&#xff0c;localStorage能够长期保存&#xff0c;sessionStorage在会话期间保存。 localStorage用法&#xff1a; 1.设置&#xff1a; v…

08-Flutter移动电商实战-dio基础_伪造请求头获取数据

08-Flutter移动电商实战-dio基础_伪造请求头获取数据 在很多时候&#xff0c;后端为了安全都会有一些请求头的限制&#xff0c;只有请求头对了&#xff0c;才能正确返回数据。这虽然限制了一些人恶意请求数据&#xff0c;但是对于我们聪明的程序员来说&#xff0c;就是形同虚设…

工作269:uni--客流分析优化

<template><view><view><view id"main"><!-- 第一步 设置盒子大小 --></view></view><view><view v-for"(item,index) in 10"><view style"display: flex;justify-content: space-between;…

No virtual method diskCacheStrategy

android运行时错误。报错如下&#xff1a; java.lang.NoSuchMethodError: No virtual method diskCacheStrategy(Lcom/bumptech/glide/load/engine/DiskCacheStrategy;)Lcom/bumptech/glide/request/BaseRequestOptions; in class Lcom/bumptech/glide/request/RequestOptions…

工作270:el-dialog的open回调

<el-dialog open"DepartThrow" title"新建部门" :visible.sync"dialogFormVisible" close"close"><el-form ref"form" :rules"rules" :model"form" size"medium" v-loading"lo…

09-Flutter移动电商实战-移动商城数据请求实战

09-Flutter移动电商实战-移动商城数据请求实战 1、URL接口管理文件建立 第一步需要在建立一个URL的管理文件&#xff0c;因为课程的接口会一直进行变化&#xff0c;所以单独拿出来会非常方便变化接口。当然工作中的URL管理也是需要这样配置的&#xff0c;以为我们会不断的切换…

栈和队列的区别,栈和堆的区别

栈和队列的区别&#xff1a; 栈的插入和删除操作都是在一端进行的&#xff0c;而队列的操作却是在两端进行的。 栈是先进后出&#xff0c;队列是先进先出。 栈只允许在表尾一端进行插入和删除&#xff0c;队列只允许在表尾一端进行插入&#xff0c;在表头一端进行删除。 栈和堆…

Android 串口开发,发送串口命令,读卡,反扫码,USB通讯,实现demo。——持续更新

应用场景&#xff1a;APP发送串口命令到打印机&#xff0c;打印相应数据小票 // 串口 implementation com.github.licheedev.Android-SerialPort-API:serialport:1.0.1 1、获取全部串口地址devicePath private String[] mDevices; public void getcuankou(){SerialPortFinder…

10-Flutter移动电商实战-使用FlutterSwiper制作轮播效果

10-Flutter移动电商实战-使用FlutterSwiper制作轮播效果 1、引入flutter_swiper插件 flutter最强大的siwiper, 多种布局方式&#xff0c;无限轮播&#xff0c;Android和IOS双端适配. 好牛X得介绍&#xff0c;一般敢用“最”的一般都是神级大神&#xff0c;看到这个介绍后我也是…

python中的分号

很多编程语言是以分号作为一行代码的的结束标志&#xff0c;但是在Python中不是这样的&#xff0c;而是靠缩进来识别程序结构。 Python中一行代码以分号结束&#xff0c;并不是必须的&#xff0c;准确来说是不被推荐的&#xff0c;因为加上分号就是画蛇添足的行为&#xff0c;吃…

工作271:打开弹出框调用当前页面接口

<template><div><button-dialogopen"open"ref"dialog"width"1000px"title"内容关联"ok-button-text"确认关联":destroy-on-close"true"ok"confirmAssociation"><!----><cus…

Android char数据类型乱码��解决方法

一般char类型是在一个循环当中使用的。同时会结合其他数据类型使用&#xff0c;如StringBuffer。通过stringBuffer.append(pressedKey);方法把循环的char组合成一个String。 通过打印数据可以看到当0转换为char类型时&#xff0c;会出现乱码&#xfffd;&#xfffd; 所以解决…

11-Flutter移动电商实战-首页_屏幕适配方案和制作

11-Flutter移动电商实战-首页_屏幕适配方案和制作 1、flutter_ScreenUtil插件简介 flutter_ScreenUtil屏幕适配方案&#xff0c;让你的UI在不同尺寸的屏幕上都能显示合理的布局。 插件会让你先设置一个UI稿的尺寸&#xff0c;他会根据这个尺寸&#xff0c;根据不同屏幕进行缩…

历史:古代:秦朝

秦&#xff0c;南进两广吞百越&#xff0c;北打匈奴连燕赵秦长城 焚书坑儒 扶苏蒙恬&#xff0c;胡亥赵高&#xff0c;指鹿为马 陈胜吴广起义 刘邦&#xff08;萧何&#xff09;斩白蛇起义&#xff0c;投靠项羽&#xff08;楚国人&#xff09; 刘项进关中&#xff0c;收张良&am…

Android 友盟推送开发

推送厂商集成文档&#xff1a;https://developer.umeng.com/docs/67966/detail/98589?spma311a.9588098.0.0#h1-vivo-push-5 SDK下载&#xff1a;https://developer.umeng.com/sdk/android 集成步骤 1、导入依赖 implementation com.umeng.sdk:common:1.5.3 implementation …

12-Flutter移动电商实战-首页导航区域编写

12-Flutter移动电商实战-首页导航区域编写 1、导航单元素的编写 从外部看&#xff0c;导航是一个GridView部件&#xff0c;但是每一个导航又是一个上下关系的Column。小伙伴们都知道Flutter有多层嵌套的问题&#xff0c;如果我们都写在一个组件里&#xff0c;那势必造成嵌套严…