美文网首页
Dio 请求代码演示

Dio 请求代码演示

作者: 叶秋_YQ | 来源:发表于2019-05-08 14:36 被阅读0次
    dio_get请求
    import 'package:flutter/material.dart';
    import 'package:dio/dio.dart';
    
    class HomePage extends StatefulWidget {
      HomePage({Key key}) : super(key: key);
    
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      TextEditingController typeController = TextEditingController();
      String showText = '欢迎来到上海白马高级会所';
      @override
      Widget build(BuildContext context) {
        return Container(
           child: Scaffold(
             appBar: AppBar(title: Text('上海白马会所'),),
             body: Container(
               child: Column(
                 children: <Widget>[
                   TextField(
                     // 监听文本字段的变化
                     controller: typeController,
                     decoration: InputDecoration(
                       labelText: '帅哥类型',
                       helperText: '请输入你喜欢的类型'
                     ),
                     // 关闭软键盘自动弹起
                     autofocus: false,
                   ),
                   RaisedButton(
                     onPressed: _choiceAction,
                     child: Text('点鸭'),
                   ),
                   Text(
                     showText,
                     overflow: TextOverflow.ellipsis,
                     maxLines: 1,
                   )
                 ],
               ),
             ),
           ) 
        );
      }
      _choiceAction(){
        // 判断文本框是否为空,如果为空就不查询
        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};
          // 这个链接是easy-mock 里面做的假数据
          // 如果使用post请求直接在这里换成post请求方式就行了
          response = await Dio().get("https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",
          queryParameters:data
          );
          return response.data;
        }catch(e){
          print(e);
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:Dio 请求代码演示

          本文链接:https://www.haomeiwen.com/subject/cjimoqtx.html