Flutter -------- Http库 网络请求封装(HttpController)
Posted zhangqie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter -------- Http库 网络请求封装(HttpController)相关的知识,希望对你有一定的参考价值。
http库 再次封装的网络请求类 HttpController
1.添加依赖
dependencies:
http: ^0.12.0 #latest version
2.导入库
import ‘package:http/http.dart‘ as http; //导入前需要配置
效果图:
封装类
import ‘package:http/http.dart‘ as http; class HttpController static void get(String url, Function callback, Map<String, String> params, Function errorCallback) async if (params != null && params.isNotEmpty) StringBuffer sb = new StringBuffer("?"); params.forEach((key, value) sb.write("$key" + "=" + "$value" + "&"); ); String paramStr = sb.toString(); paramStr = paramStr.substring(0, paramStr.length - 1); url += paramStr; try http.Response res = await http.get(url); if (callback != null) callback(res.body); catch (exception) if (errorCallback != null) errorCallback(exception); static void post(String url, Function callback, Map<String, String> params, Function errorCallback) async try http.Response res = await http.post(url, body: params); if (callback != null) callback(res.body); catch (e) if (errorCallback != null) errorCallback(e);
调用:
import ‘dart:convert‘; import ‘package:flutter/material.dart‘; import ‘package:flutter_test1/http/HttpController.dart‘; class HomePage extends StatefulWidget @override State<StatefulWidget> createState() // TODO: implement createState return new Page(); class Page extends State<HomePage> String dataStr = ""; var _items = []; @override Widget build(BuildContext context) return layout(context); @override void initState() super.initState(); getData(); void getData() Map<String, String> map = new Map(); map["v"] = "1.0"; map["month"] = "7"; map["day"] = "25"; map["key"] = "bd6e35a2691ae5bb8425c8631e475c2a"; HttpController.post("http://api.juheapi.com/japi/toh", (data) if (data != null) final body = json.decode(data.toString()); final feeds = body["result"]; var items = []; feeds.forEach((item) items.add(Model(item["_id"], item["title"], item["pic"], item["year"], item["month"], item["day"], item["des"], item["lunar"])); ); setState(() dataStr = data.toString(); _items = items; ); , params: map); Widget layout(BuildContext context) return new Scaffold( appBar: buildAppBar(context), body: new ListView.builder(itemCount: _items.length, itemBuilder: itemView), ); Widget itemView(BuildContext context, int index) Model model = this._items[index]; //设置分割线 if (index.isOdd) return new Divider(height: 2.0); return new Container( color: Color.fromARGB(0x22, 0x49, 0xa9, 0x8d), child: new Padding( padding: const EdgeInsets.all(8.0), child: new Padding( padding: const EdgeInsets.all(8.0), child: new Column( children: <Widget>[ new Row( children: <Widget>[ new Text(‘$model.year年$model.month月$model.day日‘, style: new TextStyle(fontSize: 15.0)), new Text(‘($model.lunar)‘, style: new TextStyle(fontSize: 15.0)), ], ), new Center( heightFactor: 6.0, child: new Text("$model.title", style: new TextStyle(fontSize: 17.0)), ) ], )))); Widget buildAppBar(BuildContext context) return new AppBar(title: const Text(‘历史今日‘)); class Model String _id; String title; String pic; int year; int month; int day; String des; String lunar; Model(this._id, this.title, this.pic, this.year, this.month, this.day, this.des, this.lunar);
以上是关于Flutter -------- Http库 网络请求封装(HttpController)的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 之网络请求Dio, FormData, 表单网络请求, x-www-form-urlencoded