网络请求

2020-07-02  本文已影响0人  zZ_d205

借助dio进行网络请求

import 'dart:convert';

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter/widgets.dart';

import 'package:dio/dio.dart';

void main() => runApp(MyApp());

class MyAppextends StatelessWidget {

@override

  Widget build(BuildContext context) {

return MaterialApp(

title:'Flutter Demo',

theme:ThemeData(

// This is the theme of your application.

//

// Try running your application with "flutter run". You'll see the

// application has a blue toolbar. Then, without quitting the app, try

// changing the primarySwatch below to Colors.green and then invoke

// "hot reload" (press "r" in the console where you ran "flutter run",

// or simply save your changes to "hot reload" in a Flutter IDE).

// Notice that the counter didn't reset back to zero; the application

// is not restarted.

        primarySwatch: Colors.blue,

// This makes the visual density adapt to the platform that you run

// the app on. For desktop platforms, the controls will be smaller and

// closer together (more dense) than on mobile platforms.

        visualDensity: VisualDensity.adaptivePlatformDensity,

),

home:MyP(),

);

}

}

class MyPextends StatefulWidget {

@override

  _MyPState createState() =>_MyPState();

}

class _MyPStateextends State {

List_dataRul=[];

String_data ="";

@override

  Widget build(BuildContext context) {

return Scaffold(

appBar:AppBar(

title:Text("网络请求"),

),

body:Column(

children: [

FlatButton(onPressed: ()async{

Dio dio=new Dio();

Response res=await dio.get("https://api.jisuapi.com/area/province?appkey=f4c9d04566fc7039");

//            print(res.data);

//            List items=json.decode(res.data.toString());

//            print(items);

            var data= json.decode(res.toString());//3

            List dataRul=data['result'];

dataRul.forEach((e) {

//              print(e);

//              print(e['name']);

            });

setState(() {

this._dataRul=dataRul;

});

//            print(data['result']);

          }, child:Text("请求接口")),

Container(

height:200,

child:ListView(

children:_dataRul.map((e) =>Text(e["name"])).toList() ,

) ,

)

],

),

);

}

}

//class _MyPState extends State {

//  String _data = "";

//  @override

//  Widget build(BuildContext context) {

//    return Container(

//      child: Column(

//        children: [

//          FlatButton(

//            onPressed: () async {

//              print(1111);

//              Dio dio = new Dio();

//              Response res = await dio.get('https://www.baidu.com',queryParameters: {"username":"zhansan","psd":"123"});

//              dio.post('https://www.baidu.com',data: {"username":"zhansan","psd":"123"});

//

////              下载文件  下载地址  保存地址

////              dio.download("https://www.baidu.com/logo.png", savePath)

//

//              print(res);

//              setState(() {

//                print(res.data.toString());

//                _data = res.data.toString();

//              });

//            },

//            child: Text("发起请求"),

//            color: Colors.red,

//          ),

//          Scrollbar(

//              child: Container(

//              height: 400,

//              child: SingleChildScrollView(

//                child: Text(_data),

//              ),

//          ))

//        ],

//      ),

//    );

//  }

//}

上一篇下一篇

猜你喜欢

热点阅读