為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):3738
Dart和Flutter提供了工具來(lái)完成訪問(wèn)網(wǎng)絡(luò)連接的工作。
http
包http
包來(lái)生成網(wǎng)絡(luò)請(qǐng)求http
包http
提供了最簡(jiǎn)單的房還是來(lái)訪問(wèn)網(wǎng)絡(luò)的數(shù)據(jù)。
安裝http
包,我們需要pubspec.yaml
增加依賴。我們 可以在[pub website]找到最新的版本來(lái)安裝(https://pub.dartlang.org/packages/http#-installing-tab-).
dependencies: http: <latest_version>
http
包來(lái)生成網(wǎng)絡(luò)請(qǐng)求在這個(gè)例子里,我們使用在JSONPlaceholder REST API里面的 http.get()來(lái)發(fā)送一個(gè)請(qǐng)求。
Future<http.Response> fetchPost() { return http.get('https://jsonplaceholder.typicode.com/posts/1'); }
http.get()
方法返回包含Response
的一個(gè)Future
Future
是Dart類的一個(gè)核心同步操作。它用于表示將來(lái)某個(gè)時(shí)候可用的潛在值或錯(cuò)誤。
http.Response
類包含從成功的http調(diào)用接收的數(shù)據(jù)。
雖然很容易發(fā)送一個(gè)網(wǎng)絡(luò)請(qǐng)求,但是使用原始的Future<http.Response>
并不是很便。為了讓我們的更容易處理,可以將http.Response
轉(zhuǎn)換為我們自己的Dart對(duì)象。
Post
類首先,我們需要?jiǎng)?chuàng)建一個(gè)包含來(lái)自網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)的Post
類。它還將包括一個(gè)工廠構(gòu)造器,允許我們從json創(chuàng)建Post
。
手動(dòng)轉(zhuǎn)換JSON只是其中一個(gè)選擇。有關(guān)更多信息,請(qǐng)參閱關(guān)于JSON和序列化。
class Post { final int userId; final int id; final String title; final String body;Post({this.userId, this.id, this.title, this.body});factory Post.fromJson(Map<String, dynamic> json) { return Post( userId: json['userId'], id: json['id'], title: json['title'], body: json['body'], ); } }
http.Response
轉(zhuǎn)換為Post
。現(xiàn)在,我們將更新fetchPost
函數(shù)以返回<Post>
。為此,我們需要:
1.將響應(yīng)主體轉(zhuǎn)換為帶有dart
包的jsonMap
。
2.如果服務(wù)器返回狀態(tài)碼為200的"OK"響應(yīng),則使用fromJson
工廠將jsonMap
轉(zhuǎn)換為Post
。
3.如果服務(wù)器返回意外響應(yīng),則拋出錯(cuò)誤
Future<Post> fetchPost() async { final response = await http.get('https://jsonplaceholder.typicode.com/posts/1');if (response.statusCode == 200) { // If server returns an OK response, parse the JSON return Post.fromJson(json.decode(response.body)); } else { // If that response was not OK, throw an error. throw Exception('Failed to load post'); } }
萬(wàn)歲!現(xiàn)在我們有了一個(gè)函數(shù),我們可以調(diào)用它來(lái)從互聯(lián)網(wǎng)上獲取一篇文章!
為了使獲取的數(shù)據(jù)可以在屏幕上顯示,我們使用FutureBuilder
控件。FutureBuilder
控件附帶了Flutter,使得使用異步數(shù)據(jù)源變得容易。
我們必須提供兩個(gè)參數(shù):
1.在這個(gè)例子中,我們將調(diào)用Futrue
中的fetchPost()
函數(shù)
builder
函數(shù),告訴Flutter要呈現(xiàn)什么,這取決于Future
的狀態(tài):加載、成功或錯(cuò)誤。FutureBuilder<Post>( future: fetchPost(), builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }// By default, show a loading spinner return CircularProgressIndicator(); }, );
盡管很方便,但是不建議在build()
方法中調(diào)用API。
每當(dāng)Flutter想要更改視圖中的任何內(nèi)容時(shí),它都會(huì)調(diào)用build()
方法,而這種情況經(jīng)常出人意料地發(fā)生。如果在`build()'方法中留下fetch調(diào)用,則API中將充斥著不必要的調(diào)用,并減慢應(yīng)用程序的速度。
以下是一些更好的選擇,因此它只會(huì)在頁(yè)面最初加載時(shí)命中API。
StatelessWidget
中使用這個(gè)策略,父小部件負(fù)責(zé)調(diào)用fetch方法,存儲(chǔ)其結(jié)果,然后將其傳遞給小部件。
class MyApp extends StatelessWidget { final Future<Post> post;MyApp({Key key, this.post}) : super(key: key);
您可以在下面的完整示例中看到這個(gè)示例的工作示例。
StatefulWidget
狀態(tài)的生命周期中調(diào)用它如果您的小部件是有狀態(tài)的,您可以在initState'](https://docs.flutter.io/flutter/widgets/State/initState.html)或[
didChangeDependencies'方法中調(diào)用獲取方法。
initstate
被調(diào)用一次,然后再也不會(huì)調(diào)用。如果您想選擇重新加載API以響應(yīng)[inheritedWidget
](https://docs.flutter.io/flutter/widgets/inheritedWidget class.html)更改,請(qǐng)將調(diào)用放入didchangeDependencies
方法。更多詳情請(qǐng)參見(jiàn)[state
](https://docs.flutter.io/flutter/widgets/state class.html)。
class _MyAppState extends State<MyApp> { Future<Post> post;@override void initState() { super.initState(); post = fetchPost(); }
有關(guān)如何測(cè)試此功能的信息,請(qǐng)參閱以下文章:
import 'dart:async'; import 'dart:convert';import 'package:flutter/material.dart'; import 'package:http/http.dart' as http;Future<Post> fetchPost() async { final response = await http.get('https://jsonplaceholder.typicode.com/posts/1');if (response.statusCode == 200) { // If the call to the server was successful, parse the JSON return Post.fromJson(json.decode(response.body)); } else { // If that call was not successful, throw an error. throw Exception('Failed to load post'); } }class Post { final int userId; final int id; final String title; final String body;Post({this.userId, this.id, this.title, this.body});factory Post.fromJson(Map<String, dynamic> json) { return Post( userId: json['userId'], id: json['id'], title: json['title'], body: json['body'], ); } }void main() => runApp(MyApp(post: fetchPost()));class MyApp extends StatelessWidget { final Future<Post> post;MyApp({Key key, this.post}) : super(key: key);@override Widget build(BuildContext context) { return MaterialApp( title: 'Fetch Data Example', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: Text('Fetch Data Example'), ), body: Center( child: FutureBuilder<Post>( future: post, builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }// By default, show a loading spinner return CircularProgressIndicator(); }, ), ), ), ); } }
1.Flutter初步探索(四)網(wǎng)絡(luò)連接
1.Fetch data from the internet