zoukankan      html  css  js  c++  java
  • Flutter从入门到入土(三)网络请求Dio

    1、网络请求

     1   Dio _dio = new Dio();
     2 
     3   Future getInformationJson(User user) async {
     4 
     5     var url = 'http://xxx/xxx/xxx?username=' + user._username +
     6         '&password=' + user._password;
     7 
     8     Response response = await _dio.get(url);
     9     if (response.statusCode == HttpStatus.ok) {
    10       var data = jsonDecode(response.toString());  //JSON数据的解析
    11       try{
    12         data['payload']['scores'].forEach((v) {
    13 //          print(v['className']);
    14           Scores score = new Scores(
    15               v['term'], v['className'], v['credit'].toString(), v['score'],
    16               v['gpa'].toString());
    17           user._scores.add(score);
    18         });
    19 
    20         user._avggpa = data['payload']['gpa'].toString();
    21         user._avgscore = data['payload']['avg'].toString();
    22         user._size = data['payload']['size'].toString();
    23       }catch(e){
    24         print(e);
    25       }
    26     }
    27     return user;
    28   }

    2、异步请求(在执行界面渲染的类当中)

    1   void initState() {
    2     super.initState();
    3     user = new User();
    4 
    5     information.getInformationJson(user).then((val){
    6       user = val;
    7       build(context);
    8     });
    9   }

    使用setState(() {});

     1   @override
     2   Widget build(BuildContext context){
     3     // TODO: implement build
     4 
     5     if(user._scores == null || user._scores.length == 0){
     6       return loadingWidget();
     7     }else{
     8       print('main');
     9       setState(() {});
    10       return mainWidget();
    11     }
    12   }

    当user获得到数据后就会重新渲染页面(loadingWidget ==> mainWidget)

    每天进步一点点
  • 相关阅读:
    Jquery EasyUI tabs处理
    C# ToString格式控制符
    SQL删除重复数据,保留一条
    stm32f4xx 的IWDG使用的一般步骤
    stm32f4xx 的EXTI使用的一般步骤
    STM32F4xx---EXTI 外部中断
    数组和指针 到 存储类(1)
    uCosII 从 OSStart开始到思维定势··········
    《C和指针》一书介绍操作符优先级
    OSTimeTick()函数解析
  • 原文地址:https://www.cnblogs.com/smallstars/p/12927286.html
Copyright © 2011-2022 走看看