{"id":1037,"date":"2020-05-25T19:00:23","date_gmt":"2020-05-25T13:00:23","guid":{"rendered":"https:\/\/open.zeba.academy\/?p=1037"},"modified":"2020-05-25T19:00:23","modified_gmt":"2020-05-25T13:00:23","slug":"kak-poluchit-dannye-interneta","status":"publish","type":"post","link":"https:\/\/code.zeba.academy\/ru\/kak-poluchit-dannye-interneta\/","title":{"rendered":"\u041a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430?"},"content":{"rendered":"<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, Dart \u0438 Flutter \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u0430\u043a\u0435\u0442 http, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0440\u0430\u0431\u043e\u0442\u044b.<!--more--><\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e:<\/p>\n<ol>\n<li>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043f\u0430\u043a\u0435\u0442 http.<\/li>\n<li>\u0421\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0430\u043a\u0435\u0442 http.<\/li>\n<li>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0439\u0442\u0435 \u043e\u0442\u0432\u0435\u0442 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Dart.<\/li>\n<li>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Flutter.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li>\n<h3>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043f\u0430\u043a\u0435\u0442 http<\/h3>\n<\/li>\n<\/ol>\n<p>\u041f\u0430\u043a\u0435\u0442 http \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0430\u043a\u0435\u0442 http, \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0435\u0433\u043e \u0432 \u0440\u0430\u0437\u0434\u0435\u043b \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0444\u0430\u0439\u043b\u0430 pubspec.yaml. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e http \u043f\u0430\u043a\u0435\u0442\u0430 pub.dev.<\/p>\n<pre>dependencies:\n  http: &lt;latest_version&gt;<\/pre>\n<p>\u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u043f\u0430\u043a\u0435\u0442 http.<\/p>\n<pre>import 'package:http\/http.dart' as http;<\/pre>\n<ol start=\"2\">\n<li>\n<h3>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/h3>\n<\/li>\n<\/ol>\n<p>\u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0430\u043b\u044c\u0431\u043e\u043c\u0430 \u0438\u0437 JSONPlaceholder \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 http.get ().<\/p>\n<pre>Future&lt;http.Response&gt; fetchAlbum() {\n  return http.get('https:\/\/jsonplaceholder.typicode.com\/albums\/1');\n}<\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 http.get () \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 Future, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 Response.<\/p>\n<ul>\n<li>Future &#8212; \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 Dart \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438. \u041e\u0431\u044a\u0435\u043a\u0442 Future\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c.<\/li>\n<li>\u041a\u043b\u0430\u0441\u0441 http.Response \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 http.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>\n<h3>\u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Dart<\/h3>\n<\/li>\n<\/ol>\n<p>\u0425\u043e\u0442\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043b\u0435\u0433\u043a\u043e, \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u043c Future&lt;http.Response&gt; \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u0430. \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0448\u0443 \u0436\u0438\u0437\u043d\u044c \u043f\u0440\u043e\u0449\u0435, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0439\u0442\u0435 http.Response \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 Dart.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 Album<\/h4>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043a\u043b\u0430\u0441\u0441 Album, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u043d \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 Album \u0438\u0437 JSON.<\/p>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 JSON \u0432\u0440\u0443\u0447\u043d\u0443\u044e &#8212;\u00a0 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442.<\/p>\n<pre>class Album {\n  final int userId;\n  final int id;\n  final String title;\n\n  Album({this.userId, this.id, this.title});\n\n  factory Album.fromJson(Map&lt;String, dynamic&gt; json) {\n    return Album(\n      userId: json['userId'],\n      id: json['id'],\n      title: json['title'],\n    );\n  }\n}<\/pre>\n<h4>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c http.Response \u0432 Album<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e fetchAlbum () \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c &lt;Future&lt;Album&gt;:<\/p>\n<ol>\n<li>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0439\u0442\u0435 \u0442\u0435\u043b\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 JSON Map \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u043a\u0435\u0442\u0430 dart: convert.<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 OK \u0441 \u043a\u043e\u0434\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 200, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0439\u0442\u0435 JSON Map \u0432 Album, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 fromJson ().<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 OK \u0441 \u043a\u043e\u0434\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 200, \u0432\u044b\u0434\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. (\u0414\u0430\u0436\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u00ab404 \u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e\u00bb \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0439\u0442\u0435 null.)<\/li>\n<\/ol>\n<pre>Future&lt;Album&gt; fetchAlbum() async {\n  final response = await http.get('https:\/\/jsonplaceholder.typicode.com\/albums\/1');\n\n  if (response.statusCode == 200) {\n    \/\/ If the server did return a 200 OK response,\n    \/\/ then parse the JSON.\n    return Album.fromJson(json.decode(response.body));\n  } else {\n    \/\/ If the server did not return a 200 OK response,\n    \/\/ then throw an exception.\n    throw Exception('Failed to load album');\n  }\n}<\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0430\u043b\u044c\u0431\u043e\u043c \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430.<\/p>\n<ol start=\"4\">\n<li>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435<\/h3>\n<\/li>\n<\/ol>\n<p>\u0412\u044b\u0437\u043e\u0432\u0438\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 fetch () \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 initState () \u0438\u043b\u0438 didChangeDependencies ().<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 initState () \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0431\u043e\u043b\u044c\u0448\u0435. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c API \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 InheritedWidget, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u0435 \u0432\u044b\u0437\u043e\u0432 \u0432 \u043c\u0435\u0442\u043e\u0434 didChangeDependencies().<\/p>\n<pre>class _MyAppState extends State&lt;MyApp&gt; {\n  Future&lt;Album&gt; futureAlbum;\n\n  @override\n  void initState() {\n    super.initState();\n    futureAlbum = fetchAlbum();\n}\nFuture \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435.<\/pre>\n<ol start=\"5\">\n<li>\n<h3>\u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435<\/h3>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0438\u0434\u0436\u0435\u0442 FutureBuilder. \u0412\u0438\u0434\u0436\u0435\u0442 FutureBuilder \u0438\u0434\u0435\u0442 \u0441 Flutter \u0438 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<ol>\n<li>Future, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0443\u0449\u0435\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 fetchAlbum ().<\/li>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u044f builder, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 Flutter, \u0447\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f Future: \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u0443\u0441\u043f\u0435\u0445 \u0438\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430.<\/li>\n<\/ol>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e snapshot.hasData \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 true \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 snapshot \u043d\u0435 null. \u0412\u043e\u0442 \u043f\u043e\u0447\u0435\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044f fetchAlbum \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u0436\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u00ab404 Not Found\u00bb. \u0415\u0441\u043b\u0438 fetchAlbum \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 null, \u0442\u043e \u0438\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e.<\/p>\n<pre>FutureBuilder&lt;Album&gt;(\n  future: futureAlbum,\n  builder: (context, snapshot) {\n    if (snapshot.hasData) {\n      return Text(snapshot.data.title);\n    } else if (snapshot.hasError) {\n      return Text(\"${snapshot.error}\");\n    }\n\n    \/\/ By default, show a loading spinner.\n    return CircularProgressIndicator();\n  },\n);<\/pre>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 fetchAlbum() \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 initState()?<\/h3>\n<p>\u0425\u043e\u0442\u044f \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 API \u0432 \u043c\u0435\u0442\u043e\u0434 build().<\/p>\n<p>Flutter \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 build() \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438, \u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e. \u041e\u0441\u0442\u0430\u0432\u0438\u0432 \u0432\u044b\u0437\u043e\u0432 fetch \u0432 \u0432\u0430\u0448\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 build(), \u0432\u044b \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0435 API \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u043c\u0438 \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u0438 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0443 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<h3>\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<pre>import 'dart:async';\nimport 'dart:convert';\n\nimport 'package:flutter\/material.dart';\nimport 'package:http\/http.dart' as http;\n\nFuture&lt;Album&gt; fetchAlbum() async {\n  final response =\n      await http.get('https:\/\/jsonplaceholder.typicode.com\/albums\/1');\n\n  if (response.statusCode == 200) {\n    \/\/ If the server did return a 200 OK response,\n    \/\/ then parse the JSON.\n    return Album.fromJson(json.decode(response.body));\n  } else {\n    \/\/ If the server did not return a 200 OK response,\n    \/\/ then throw an exception.\n    throw Exception('Failed to load album');\n  }\n}\n\nclass Album {\n  final int userId;\n  final int id;\n  final String title;\n\n  Album({this.userId, this.id, this.title});\n\n  factory Album.fromJson(Map&lt;String, dynamic&gt; json) {\n    return Album(\n      userId: json['userId'],\n      id: json['id'],\n      title: json['title'],\n    );\n  }\n}\n\nvoid main() =&gt; runApp(MyApp());\n\nclass MyApp extends StatefulWidget {\n  MyApp({Key key}) : super(key: key);\n\n  @override\n  _MyAppState createState() =&gt; _MyAppState();\n}\n\nclass _MyAppState extends State&lt;MyApp&gt; {\n  Future&lt;Album&gt; futureAlbum;\n\n  @override\n  void initState() {\n    super.initState();\n    futureAlbum = fetchAlbum();\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Fetch Data Example',\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      home: Scaffold(\n        appBar: AppBar(\n          title: Text('Fetch Data Example'),\n        ),\n        body: Center(\n          child: FutureBuilder&lt;Album&gt;(\n            future: futureAlbum,\n            builder: (context, snapshot) {\n              if (snapshot.hasData) {\n                return Text(snapshot.data.title);\n              } else if (snapshot.hasError) {\n                return Text(\"${snapshot.error}\");\n              }\n\n              \/\/ By default, show a loading spinner.\n              return CircularProgressIndicator();\n            },\n          ),\n        ),\n      ),\n    );\n  }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, Dart \u0438 Flutter \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u0430\u043a\u0435\u0442 http, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n","protected":false},"author":7,"featured_media":1039,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":" %%post_title%% %%sep%% %%sitetitle%%","_seopress_titles_desc":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, Dart \u0438 Flutter \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u0430","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"none","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":301,"_seopress_analysis_target_kw":"Dart,Flutter,\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430,fetching data,internet,\u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442,http","footnotes":""},"categories":[4],"tags":[32,46,61],"class_list":["post-1037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-dart","tag-flutter","tag-http"],"_links":{"self":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":0,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/media\/1039"}],"wp:attachment":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}