Flutter网络请求Dio使用

import 'package:dio/dio.dart';

//我的
class MyMy extends StatefulWidget {
  List gedans = new List();
  void getHttp() async {
    try {
      Response response;
      response =
          await Dio().get("http://192.168.1.16:8080/querySongListMusics");
      print("请求json-----------$response");
 //解析
      SongListMusic as = SongListMusic.fromJson(response.data);
      List songs = as.songs;
	//遍历
      for (int i = 0; i < songs.length; i++) {
        print(songs[i].image);
		//把数据设置到组件里面
        setSongListToWidget(
            songs[i].image, songs[i].songName, songs[i].id, songs[i].musics);
      }

      return print(response);
    } catch (e) {
      print("异常-----------$e");
      return print(e);
    }
  }
  //网络数据。 
  setSongListToWidget(
      String img, String name, int size, List list) async {
    gedans.add(Item_GeDan(img, name, list.length, list));
  }

  #网络请求
  dio: ^2.1.7
      ListView(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          children: gedans,
        )
        //自建的歌单、收藏的歌单
      ],

简单记录下,

你可能感兴趣的:(Flutter)