近年来各大公司对Flutter技能的要求越来越高,甚至设立了专门岗位,但掌握Flutter高阶技能的人才寥寥无几,市面上干货Flutter高阶课程少之又少,导致Flutter高阶人才缺口大。为此我们专门为大家设计了这门课程,助力你早日成为企业抢手的新一代工程师

课程的亮点:
从原理剖析到性能调优快速吸收Flutter精髓点亮高阶实践技能
从复杂APP业务需求出发遵循行业标准高度匹配大厂Flutter岗位技能需求
高阶人才不能只停留在开发整体架构设计→开发细节→效率工具方法带你完成真正的全盘思考

Flutter 是 Google 用以帮助开发者在 iOS 和 Android 两个平台开发高质量原生 UI 的移动 SDK。Flutter 兼容现有的代码,免费且开源,在全球开发者中广泛被使用。本专栏将不定期更新 Flutter 相关高级学习教程。

Flutter的网络交互
用Flutter进行网络交互有四个步骤:

导入http库
使用http库创建网络请求
将服务器相应转换为一个自定义的Dart对象
将数据展示出来
导入http库
http库是Dart团队开发的方便网络请求的库,我们需要在pubspec.yaml文件中引用它。

dependencies:
  http: <latest_version>


创建网络请求
有了网络请求库之后,接下来就该使用库进行网络请求了。

比如GET请求:

Future<http.Response> fetchPost() {
  return http.get('https://jsonplaceholder.typicode.com/posts/1');
}


POST请求:

import 'package:http/http.dart' as http;

var url = "http://example.com/whatsit/create";
http.post(url, body: {"name": "doodle", "color": "blue"})
    .then((response) {
  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");
});


创建一个Client:

var client = new http.Client();
client.post(
    "http://example.com/whatsit/create",
    body: {"name": "doodle", "color": "blue"})
  .then((response) => client.get(response.bodyFields['uri']))
  .then((response) => print(response.body))
  .whenComplete(client.close);


增加UA:

class UserAgentClient extends http.BaseClient {
  final String userAgent;
  final http.Client _inner;

  UserAgentClient(this.userAgent, this._inner);

  Future<StreamedResponse> send(BaseRequest request) {
    request.headers['user-agent'] = userAgent;
    return _inner.send(request);
  }
}
03-28 00:45