CINXE.COM
Fetch data from the internet | Dart
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><script>!function(e,t,a,n){e[n]=e[n]||[],e[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var g=t.getElementsByTagName(a)[0],m=t.createElement(a);m.async=!0,m.src="https://www.googletagmanager.com/gtm.js?id=GTM-5VSZM5J",g.parentNode.insertBefore(m,g)}(window,document,"script","dataLayer")</script><meta name="description" content="Fetch data over the internet using the http package."><title>Fetch data from the internet | Dart</title><link rel="icon" sizes="64x64" href="/assets/img/logo/dart-64.png" eleventy:ignore><link href="/assets/img/touch-icon-iphone.png" rel="apple-touch-icon" eleventy:ignore><link href="/assets/img/touch-icon-ipad.png" rel="apple-touch-icon" sizes="152x152" eleventy:ignore><link href="/assets/img/touch-icon-iphone-retina.png" rel="apple-touch-icon" sizes="180x180" eleventy:ignore><link href="/assets/img/touch-icon-ipad-retina.png" rel="apple-touch-icon" sizes="167x167" eleventy:ignore><meta name="twitter:card" content="summary"><meta name="twitter:site" content="@dart_lang"><meta name="twitter:title" content="Fetch data from the internet"><meta name="twitter:description" content="Fetch data over the internet using the http package."><meta property="og:title" content="Fetch data from the internet"><meta property="og:description" content="Fetch data over the internet using the http package."><meta property="og:url" content="/tutorials/server/fetch-data/"><meta property="og:image" content="/assets/img/logo/dart-logo-for-shares.png?2" eleventy:ignore><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Display:wght@400&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Mono:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet"><link rel="stylesheet" href="/assets/css/main.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.2/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous" referrerpolicy="no-referrer"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.5/js.cookie.min.js" integrity="sha512-nlp9/l96/EpjYBx7EP7pGASVXNe80hGhYAUrjeXnu/fyF5Py0/RXav4BBNs7n5Hx1WFhOEOWSAVjGeC3oKxDVQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><script src="/assets/js/os-tabs.js"></script><script src="/assets/js/utilities.js"></script><script src="/assets/js/main.js"></script><script defer="defer" src="/assets/js/inject_dartpad.js"></script><script>!function(e,a,t,n,c,o,s){e.GoogleAnalyticsObject=c,e[c]=e[c]||function(){(e[c].q=e[c].q||[]).push(arguments)},e[c].l=1*new Date,o=a.createElement(t),s=a.getElementsByTagName(t)[0],o.async=1,o.src="//www.google-analytics.com/analytics.js",s.parentNode.insertBefore(o,s)}(window,document,"script",0,"ga"),ga("create","UA-26406144-4","auto"),ga("send","pageview")</script></head><body class="default.html show_banner"><a id="skip" href="#site-content-title">Skip to main content</a><section id="cookie-notice"><div class="container"><p>dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic. <a href="https://policies.google.com/technologies/cookies" target="_blank" rel="noopener">Learn more</a>.</p><button id="cookie-consent" class="btn btn-primary">OK, got it</button></div></section><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5VSZM5J" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><header id="page-header" class="site-header"><nav id="mainnav" class="site-header"><div id="menu-toggle"><i class="material-symbols">menu</i></div><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart"></a><ul class="navbar"><li><a href="/overview" class="nav-link">Overview</a></li><li class="mainnav__get-started"><a href="/guides" class="nav-link active"><span>Docs</span></a></li><li><a href="/community" class="nav-link">Community</a></li><li><a href="/#try-dart" class="nav-link">Try Dart</a></li><li><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="searchfield"><form action="/search" class="site-header__search form-inline" id="cse-search-box"><input type="hidden" name="cx" value="011220921317074318178:_yy-tmb5t_i"> <input type="hidden" name="ie" value="UTF-8"> <input type="hidden" name="hl" value="en"> <input class="site-header__searchfield form-control search-field" type="search" name="q" id="search-main" autocomplete="off" placeholder="Search" aria-label="Search"></form></li></ul></nav></header><div class="banner"><p class="banner__text">Announcing Dart 3.5 and an updated Dart roadmap! <a href="https://medium.com/dartlang/dart-3-5-6ca36259fa2f" target="_blank">Learn more</a></p></div><div id="sidenav"><form action="/search/" class="site-header__search form-inline"><input class="site-header__searchfield form-control search-field" type="search" name="q" id="search-side" autocomplete="off" placeholder="Search" aria-label="Search"></form><div class="site-sidebar"><ul class="navbar-nav"><li class="nav-item"><a href="/overview" class="nav-link">Overview</a></li><li class="nav-item"><a href="/community" class="nav-link">Community</a></li><li class="nav-item"><a href="https://dartpad.dev" class="nav-link">Try Dart</a></li><li class="nav-item"><a href="/get-dart" class="nav-link">Get Dart</a></li><li class="nav-item"><a href="/guides" class="nav-link">Docs</a></li><li aria-hidden="true"><div class="sidebar-primary-divider"></div></li></ul><ul class="nav flex-column"><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-1" role="button" aria-expanded="false" aria-controls="sidenav-1">Language</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1"><li class="nav-item"><a class="nav-link" href="/language">Introduction</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-2" href="#sidenav-1-2" role="button" aria-expanded="false" aria-controls="sidenav-1-2">Syntax basics</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-2"><li class="nav-item"><a class="nav-link" href="/language/variables">Variables</a></li><li class="nav-item"><a class="nav-link" href="/language/operators">Operators</a></li><li class="nav-item"><a class="nav-link" href="/language/comments">Comments</a></li><li class="nav-item"><a class="nav-link" href="/language/metadata">Metadata</a></li><li class="nav-item"><a class="nav-link" href="/language/libraries">Libraries & imports</a></li><li class="nav-item"><a class="nav-link" href="/language/keywords">Keywords</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-3" href="#sidenav-1-3" role="button" aria-expanded="false" aria-controls="sidenav-1-3">Types</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-3"><li class="nav-item"><a class="nav-link" href="/language/built-in-types">Built-in types</a></li><li class="nav-item"><a class="nav-link" href="/language/records">Records</a></li><li class="nav-item"><a class="nav-link" href="/language/collections">Collections</a></li><li class="nav-item"><a class="nav-link" href="/language/generics">Generics</a></li><li class="nav-item"><a class="nav-link" href="/language/typedefs">Typedefs</a></li><li class="nav-item"><a class="nav-link" href="/language/type-system">Type system</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-4" href="#sidenav-1-4" role="button" aria-expanded="false" aria-controls="sidenav-1-4">Patterns</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-4"><li class="nav-item"><a class="nav-link" href="/language/patterns">Overview & usage</a></li><li class="nav-item"><a class="nav-link" href="/language/pattern-types">Pattern types</a></li><li class="nav-item"><a class="nav-link" href="https://codelabs.developers.google.com/codelabs/dart-patterns-records" target="_blank" rel="noopener">Applied tutorial</a></li></ul></li><li class="nav-item"><a class="nav-link" href="/language/functions">Functions</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-6" href="#sidenav-1-6" role="button" aria-expanded="false" aria-controls="sidenav-1-6">Control flow</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-6"><li class="nav-item"><a class="nav-link" href="/language/loops">Loops</a></li><li class="nav-item"><a class="nav-link" href="/language/branches">Branches</a></li><li class="nav-item"><a class="nav-link" href="/language/error-handling">Error handling</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-7" href="#sidenav-1-7" role="button" aria-expanded="false" aria-controls="sidenav-1-7">Classes & objects</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-7"><li class="nav-item"><a class="nav-link" href="/language/classes">Classes</a></li><li class="nav-item"><a class="nav-link" href="/language/constructors">Constructors</a></li><li class="nav-item"><a class="nav-link" href="/language/methods">Methods</a></li><li class="nav-item"><a class="nav-link" href="/language/extend">Extend a class</a></li><li class="nav-item"><a class="nav-link" href="/language/mixins">Mixins</a></li><li class="nav-item"><a class="nav-link" href="/language/enums">Enums</a></li><li class="nav-item"><a class="nav-link" href="/language/extension-methods">Extension methods</a></li><li class="nav-item"><a class="nav-link" href="/language/extension-types">Extension types</a></li><li class="nav-item"><a class="nav-link" href="/language/callable-objects">Callable objects</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-8" href="#sidenav-1-8" role="button" aria-expanded="false" aria-controls="sidenav-1-8">Class modifiers</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-8"><li class="nav-item"><a class="nav-link" href="/language/class-modifiers">Overview & usage</a></li><li class="nav-item"><a class="nav-link" href="/language/class-modifiers-for-apis">Class modifiers for API maintainers</a></li><li class="nav-item"><a class="nav-link" href="/language/modifier-reference">Reference</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-9" href="#sidenav-1-9" role="button" aria-expanded="false" aria-controls="sidenav-1-9">Concurrency</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-9"><li class="nav-item"><a class="nav-link" href="/language/concurrency">Overview</a></li><li class="nav-item"><a class="nav-link" href="/language/async">Asynchronous support</a></li><li class="nav-item"><a class="nav-link" href="/language/isolates">Isolates</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-1-10" href="#sidenav-1-10" role="button" aria-expanded="false" aria-controls="sidenav-1-10">Null safety</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-1-10"><li class="nav-item"><a class="nav-link" href="/null-safety">Sound null safety</a></li><li class="nav-item"><a class="nav-link" href="/null-safety/migration-guide">Migrating to null safety</a></li><li class="nav-item"><a class="nav-link" href="/null-safety/understanding-null-safety">Understanding null safety</a></li><li class="nav-item"><a class="nav-link" href="/null-safety/unsound-null-safety">Unsound null safety</a></li><li class="nav-item"><a class="nav-link" href="/null-safety/faq">FAQ</a></li></ul></li></ul></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-2" role="button" aria-expanded="false" aria-controls="sidenav-2">Core libraries</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-2"><li class="nav-item"><a class="nav-link" href="/libraries">Overview</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-core">dart:core</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-async">dart:async</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-math">dart:math</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-convert">dart:convert</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-io">dart:io</a></li><li class="nav-item"><a class="nav-link" href="/libraries/dart-html">dart:html</a></li><div class="dropdown-divider"></div><li class="nav-item"><a class="nav-link" href="/libraries/collections/iterables">Iterable collections</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-2-10" href="#sidenav-2-10" role="button" aria-expanded="false" aria-controls="sidenav-2-10">Asynchronous programming</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-2-10"><li class="nav-item"><a class="nav-link" href="/libraries/async/async-await">Tutorial</a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/futures-error-handling">Futures and error handling</a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/using-streams">Using streams</a></li><li class="nav-item"><a class="nav-link" href="/libraries/async/creating-streams">Creating streams</a></li></ul></li></ul></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-3" role="button" aria-expanded="false" aria-controls="sidenav-3">Effective Dart</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-3"><li class="nav-item"><a class="nav-link" href="/effective-dart">Overview</a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/style">Style</a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/documentation">Documentation</a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/usage">Usage</a></li><li class="nav-item"><a class="nav-link" href="/effective-dart/design">Design</a></li></ul></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-4" role="button" aria-expanded="false" aria-controls="sidenav-4">Packages</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-4"><li class="nav-item"><a class="nav-link" href="/tools/pub/packages">How to use packages</a></li><li class="nav-item"><a class="nav-link" href="/resources/useful-packages">Commonly used packages</a></li><li class="nav-item"><a class="nav-link" href="/guides/libraries/create-packages">Creating packages</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/publishing">Publishing packages</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/writing-package-pages">Writing package pages</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-4-6" href="#sidenav-4-6" role="button" aria-expanded="false" aria-controls="sidenav-4-6">Package reference</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-4-6"><li class="nav-item"><a class="nav-link" href="/tools/pub/dependencies">Dependencies</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/glossary">Glossary</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/package-layout">Package layout conventions</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/environment-variables">Pub environment variables</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/pubspec">Pubspec file</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/troubleshoot">Troubleshooting pub</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/verified-publishers">Verified publishers</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/security-advisories">Security advisories</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/versioning">Versioning</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/custom-package-repositories">Custom package repositories</a></li></ul></li><li class="nav-item"><a class="nav-link" href="/guides/libraries/private-files">What not to commit</a></li></ul></li><li class="nav-item"><a class="nav-link active collapsible" data-toggle="collapse" href="#sidenav-5" role="button" aria-expanded="true" aria-controls="sidenav-5">Development</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-5"><li class="nav-item"><a class="nav-link" href="/guides/json">JSON</a></li><li class="nav-item"><a class="nav-link" href="/guides/language/numbers">Number representation</a></li><li class="nav-item"><a class="nav-link" href="/resources/google-apis">Google APIs</a></li><li class="nav-item"><a class="nav-link" href="/multiplatform-apps">Multi-platform apps</a></li><li class="nav-item"><a class="nav-link active collapsible" data-toggle="collapse" data-target="#sidenav-5-5" href="#sidenav-5-5" role="button" aria-expanded="true" aria-controls="sidenav-5-5">Command-line & server apps</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-5-5"><li class="nav-item"><a class="nav-link" href="/server">Overview</a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/get-started">Get started</a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/cmdline">Write command-line apps</a></li><li class="nav-item"><a class="nav-link active" href="/tutorials/server/fetch-data">Fetch data from the internet</a></li><li class="nav-item"><a class="nav-link" href="/tutorials/server/httpserver">Write HTTP servers</a></li><li class="nav-item"><a class="nav-link" href="/server/libraries">Libraries & packages</a></li><li class="nav-item"><a class="nav-link" href="/server/google-cloud">Google Cloud</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-5-6" href="#sidenav-5-6" role="button" aria-expanded="false" aria-controls="sidenav-5-6">Web apps</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-5-6"><li class="nav-item"><a class="nav-link" href="/web">Overview</a></li><li class="nav-item"><a class="nav-link" href="/web/get-started">Get started</a></li><li class="nav-item"><a class="nav-link" href="/web/deployment">Deployment</a></li><li class="nav-item"><a class="nav-link" href="/web/libraries">Libraries & packages</a></li><li class="nav-item"><a class="nav-link" href="/web/wasm">Wasm compilation</a></li></ul></li><li class="nav-item"><a class="nav-link" href="/guides/environment-declarations">Environment declarations</a></li></ul></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-6" role="button" aria-expanded="false" aria-controls="sidenav-6">Interoperability</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-6"><li class="nav-item"><a class="nav-link" href="/interop/c-interop">C interop</a></li><li class="nav-item"><a class="nav-link" href="/interop/objective-c-interop">Objective-C & Swift interop</a></li><li class="nav-item"><a class="nav-link" href="/interop/java-interop">Java & Kotlin interop</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-6-4" href="#sidenav-6-4" role="button" aria-expanded="false" aria-controls="sidenav-6-4">JavaScript interop</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-6-4"><li class="nav-item"><a class="nav-link" href="/interop/js-interop">Overview</a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/usage">Usage</a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/js-types">JS types</a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/tutorials">Tutorials</a></li><li class="nav-item"><a class="nav-link" href="/interop/js-interop/past-js-interop">Past JS interop</a></li><div class="dropdown-divider"></div><li class="nav-item"><a class="nav-link" href="/interop/js-interop/package-web">Web interop</a></li></ul></li></ul></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-7" role="button" aria-expanded="false" aria-controls="sidenav-7">Tools & techniques</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7"><li class="nav-item"><a class="nav-link" href="/tools">Overview</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-7-2" href="#sidenav-7-2" role="button" aria-expanded="false" aria-controls="sidenav-7-2">Editors & debuggers</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7-2"><li class="nav-item"><a class="nav-link" href="/tools/jetbrains-plugin">IntelliJ & Android Studio</a></li><li class="nav-item"><a class="nav-link" href="/tools/vs-code">VS Code</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-devtools">Dart DevTools</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-7-2-4" href="#sidenav-7-2-4" role="button" aria-expanded="false" aria-controls="sidenav-7-2-4">DartPad</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7-2-4"><li class="nav-item"><a class="nav-link" href="/tools/dartpad">Overview</a></li><li class="nav-item"><a class="nav-link" href="/tools/dartpad/troubleshoot">Troubleshooting DartPad</a></li></ul></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-7-3" href="#sidenav-7-3" role="button" aria-expanded="false" aria-controls="sidenav-7-3">Command-line tools</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7-3"><li class="nav-item"><a class="nav-link collapsible" data-toggle="collapse" data-target="#sidenav-7-3-1" href="#sidenav-7-3-1" role="button" aria-expanded="true" aria-controls="sidenav-7-3-1">Dart SDK</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-7-3-1"><li class="nav-item"><a class="nav-link" href="/tools/sdk">Overview</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-tool">dart</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-analyze">dart analyze</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-compile">dart compile</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-create">dart create</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-doc">dart doc</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-fix">dart fix</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-format">dart format</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-info">dart info</a></li><li class="nav-item"><a class="nav-link" href="/tools/pub/cmd">dart pub</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-run">dart run</a></li><li class="nav-item"><a class="nav-link" href="/tools/dart-test">dart test</a></li><li class="nav-item"><a class="nav-link" href="/tools/dartaotruntime">dartaotruntime</a></li><li class="nav-item"><a class="nav-link" href="/tools/experiment-flags">Experiment flags</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible" data-toggle="collapse" data-target="#sidenav-7-3-2" href="#sidenav-7-3-2" role="button" aria-expanded="true" aria-controls="sidenav-7-3-2">Other command-line tools</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-7-3-2"><li class="nav-item"><a class="nav-link" href="/tools/build_runner">build_runner</a></li><li class="nav-item"><a class="nav-link" href="/tools/webdev">webdev</a></li></ul></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-7-4" href="#sidenav-7-4" role="button" aria-expanded="false" aria-controls="sidenav-7-4">Static analysis</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7-4"><li class="nav-item"><a class="nav-link" href="/tools/analysis">Customizing static analysis</a></li><li class="nav-item"><a class="nav-link" href="/guides/language/sound-problems">Fixing common type problems</a></li><li class="nav-item"><a class="nav-link" href="/tools/non-promotion-reasons">Fixing type promotion failures</a></li><li class="nav-item"><a class="nav-link" href="/tools/linter-rules">Linter rules</a></li><li class="nav-item"><a class="nav-link" href="/tools/diagnostic-messages">Diagnostic messages</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-7-5" href="#sidenav-7-5" role="button" aria-expanded="false" aria-controls="sidenav-7-5">Testing & optimization</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-7-5"><li class="nav-item"><a class="nav-link" href="/guides/testing">Testing</a></li><li class="nav-item"><a class="nav-link" href="/web/debugging">Debugging web apps</a></li></ul></li></ul></li><li aria-hidden="true"><div class="sidebar-primary-divider"></div></li><li class="nav-item"><a class="nav-link collapsed collapsible" data-toggle="collapse" href="#sidenav-9" role="button" aria-expanded="false" aria-controls="sidenav-9">Resources</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-9"><li class="nav-item"><a class="nav-link" href="/resources/dart-cheatsheet">Language cheatsheet</a></li><li class="nav-item"><a class="nav-link" href="/resources/breaking-changes">Breaking changes</a></li><li class="nav-item"><a class="nav-link" href="/guides/language/evolution">Language evolution</a></li><li class="nav-item"><a class="nav-link" href="/guides/language/spec">Language specification</a></li><li class="nav-item"><a class="nav-link" href="/resources/dart-3-migration">Dart 3 migration guide</a></li><li class="nav-item"><a class="nav-link collapsible collapsed" data-toggle="collapse" data-target="#sidenav-9-6" href="#sidenav-9-6" role="button" aria-expanded="false" aria-controls="sidenav-9-6">Coming from ...</a><ul class="nav flex-column flex-nowrap collapse" id="sidenav-9-6"><li class="nav-item"><a class="nav-link" href="/resources/coming-from/js-to-dart">JavaScript to Dart</a></li><li class="nav-item"><a class="nav-link" href="/resources/coming-from/swift-to-dart">Swift to Dart</a></li></ul></li><div class="dropdown-divider"></div><li class="nav-item"><a class="nav-link" href="/resources/faq">FAQ</a></li><li class="nav-item"><a class="nav-link" href="/resources/glossary">Glossary</a></li><li class="nav-item"><a class="nav-link" href="/resources/books">Books</a></li><li class="nav-item"><a class="nav-link" href="/resources/videos">Videos</a></li><li class="nav-item"><a class="nav-link" href="/tutorials">Tutorials</a></li></ul></li><li class="nav-item"><a class="nav-link collapsible" data-toggle="collapse" href="#sidenav-10" role="button" aria-expanded="true" aria-controls="sidenav-10">Related sites</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-10"><li class="nav-item"><a class="nav-link" href="https://api.dart.dev" target="_blank" rel="noopener">API reference</a></li><li class="nav-item"><a class="nav-link" href="https://medium.com/dartlang" target="_blank" rel="noopener">Blog</a></li><li class="nav-item"><a class="nav-link" href="https://dartpad.dev" target="_blank" rel="noopener">DartPad (online editor)</a></li><li class="nav-item"><a class="nav-link" href="https://flutter.dev" target="_blank" rel="noopener">Flutter</a></li><li class="nav-item"><a class="nav-link" href="https://pub.dev" target="_blank" rel="noopener">Package site</a></li></ul></li></ul></div></div><main id="page-content"><div id="site-toc--side" class="site-toc"><header class="site-toc__title">Contents</header><ul class="section-nav"><li class="toc-entry nav-item"><a class="nav-link" href="#background-concepts">Background concepts</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#json">JSON</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#http-requests">HTTP requests</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#uris-and-urls">URIs and URLs</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#retrieve-the-necessary-dependencies">Retrieve the necessary dependencies</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#build-a-url">Build a URL</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#make-a-network-request">Make a network request</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#make-multiple-requests">Make multiple requests</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#decode-the-retrieved-data">Decode the retrieved data</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#create-a-structured-class-to-store-the-data">Create a structured class to store the data</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#encode-the-data-into-your-class">Encode the data into your class</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#convert-the-response-to-an-object-of-your-structured-class">Convert the response to an object of your structured class</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#utilize-the-converted-data">Utilize the converted data</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#what-next">What next?</a></li></ul></div><article><div class="content"><nav id="subnav"><ul><li class="previous"><a href="/tutorials/server/cmdline">⟨ Write command-line apps</a></li><li class="next"><a href="/tutorials/server/httpserver">Write HTTP servers ⟩</a></li></ul></nav><div id="site-content-title"><h1>Fetch data from the internet</h1></div><div id="site-toc--inline" class="site-toc toc-collapsible toc-collapsed"><header class="site-toc__title">Contents <span class="site-toc--inline__toggle toc-toggle-down"><i class="material-symbols">keyboard_arrow_down</i></span> <span class="site-toc--inline__toggle toc-toggle-up"><i class="material-symbols">keyboard_arrow_up</i></span></header><ul class="section-nav"><li class="toc-entry"><a href="#background-concepts">Background concepts</a><ul><li class="toc-entry"><a href="#json">JSON</a></li><li class="toc-entry"><a href="#http-requests">HTTP requests</a></li><li class="toc-entry"><a href="#uris-and-urls">URIs and URLs</a></li></ul></li><li class="toc-entry"><a href="#retrieve-the-necessary-dependencies">Retrieve the necessary dependencies</a></li><li class="toc-entry"><a href="#build-a-url">Build a URL</a></li><li class="toc-entry"><a href="#make-a-network-request">Make a network request</a><ul><li class="toc-entry"><a href="#make-multiple-requests">Make multiple requests</a></li></ul></li><li class="toc-entry"><a href="#decode-the-retrieved-data">Decode the retrieved data</a><ul><li class="toc-entry"><a href="#create-a-structured-class-to-store-the-data">Create a structured class to store the data</a></li><li class="toc-entry"><a href="#encode-the-data-into-your-class">Encode the data into your class</a></li><li class="toc-entry"><a href="#convert-the-response-to-an-object-of-your-structured-class">Convert the response to an object of your structured class</a></li></ul></li><li class="toc-entry"><a href="#utilize-the-converted-data">Utilize the converted data</a></li><li class="toc-entry"><a href="#what-next">What next?</a></li></ul><span class="site-toc--inline__toggle toc-toggle-more-items"><i class="material-symbols">more_horiz</i></span></div> <?code-excerpt path-base="fetch_data"?> <aside class="alert alert-secondary"><div class="alert-header"><span>What you'll learn</span></div><div class="alert-content"><ul><li>The basics of what HTTP requests and URIs are and what they are used for.</li><li>Making HTTP requests using <code>package:http</code>.</li><li>Decoding JSON strings into Dart objects with <code>dart:convert</code>.</li><li>Converting JSON objects into class-based structures.</li></ul></div></aside><p>Most applications require some form of communication or data retrieval from the internet. Many apps do so through HTTP requests, which are sent from a client to a server to perform a specific action for a resource identified through a <a href="https://wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a> (Uniform Resource Identifier).</p><p>Data communicated over HTTP can technically be in any form, but using <a href="https://www.json.org/">JSON</a> (JavaScript Object Notation) is a popular choice due to its human-readability and language independent nature. The Dart SDK and ecosystem also have extensive support for JSON with multiple options to best meet your app's requirements.</p><p>In this tutorial, you will learn more about HTTP requests, URIs, and JSON. Then you will learn how to use <a href="https://pub.dev/packages/http"><code>package:http</code></a> as well as Dart's JSON support in the <a href="https://api.dart.dev/dart-convert/dart-convert-library.html"><code>dart:convert</code></a> library to fetch, decode, then use JSON-formatted data retrieved from an HTTP server.</p><div class="header-wrapper"><h2 id="background-concepts">Background concepts</h2><a class="heading-link" href="#background-concepts" aria-label="Link to 'Background concepts' section">#</a></div><p>The following sections provide some extra background and information around the technologies and concepts used in the tutorial to facilitate fetching data from the server. To skip directly to the tutorial content, see <a href="#retrieve-the-necessary-dependencies">Retrieve the necessary dependencies</a>.</p><div class="header-wrapper"><h3 id="json">JSON</h3><a class="heading-link" href="#json" aria-label="Link to 'JSON' section">#</a></div><p>JSON (JavaScript Object Notation) is a data-interchange format that has become ubiquitous across application development and client-server communication. It is lightweight but also easy for humans to read and write due to being text based. With JSON, various data types and simple data structures such as lists and maps can be serialized and represented by strings.</p><p>Most languages have many implementations and parsers have become extremely fast, so you don't need to worry about interoperability or performance. For more information about the JSON format, see <a href="https://www.json.org/">Introducing JSON</a>. To learn more about working with JSON in Dart, see the <a href="/guides/json">Using JSON</a> guide.</p><aside class="alert alert-secondary"><div class="alert-header"><span></span></div><div class="alert-content"><p>Two other packages exist with platform-specific implementations for mobile.</p><ul><li><a href="https://pub.dev/packages/cronet_http">cronet_http</a> provides access to the Android <a href="https://developer.android.com/develop/connectivity/cronet">Cronet</a> HTTP client.</li><li><a href="https://pub.dev/packages/cupertino_http">cupertino_http</a> provides access to Apple's <a href="https://developer.apple.com/documentation/foundation/url_loading_system">Foundation URL Loading System</a>.</li></ul><p>To learn more about their capabilities, consult the package documentation.</p></div></aside><div class="header-wrapper"><h3 id="http-requests">HTTP requests</h3><a class="heading-link" href="#http-requests" aria-label="Link to 'HTTP requests' section">#</a></div><p>HTTP (Hypertext Transfer Protocol) is a stateless protocol designed for transmitting documents, originally between web clients and web servers. You interacted with the protocol to load this page, as your browser uses an HTTP <code>GET</code> request to retrieve the contents of a page from a web server. Since its introduction, use of the HTTP protocol and its various versions have expanded to applications outside the web as well, essentially wherever communication from a client to a server is needed.</p><p>HTTP requests sent from the client to communicate with the server are composed of multiple components. HTTP libraries, such as <code>package:http</code>, allow you to specify the following kinds of communication:</p><ul><li>An HTTP method defining the desired action, such as <code>GET</code> to retrieve data or <code>POST</code> to submit new data.</li><li>The location of the resource through a URI.</li><li>The version of HTTP being used.</li><li>Headers that provide extra information to the server.</li><li>An optional body, so the request can send data to the server, not just retrieve it.</li></ul><p>To learn more about the HTTP protocol, check out <a href="https://developer.mozilla.org/docs/Web/HTTP/Overview">An overview of HTTP</a> on the mdn web docs.</p><div class="header-wrapper"><h3 id="uris-and-urls">URIs and URLs</h3><a class="heading-link" href="#uris-and-urls" aria-label="Link to 'URIs and URLs' section">#</a></div><p>To make an HTTP request, you need to provide a <a href="https://wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a> (Uniform Resource Identifier) to the resource. A URI is a character string that uniquely identifies a resource. A URL (Uniform Resource Locator) is a specific kind of URI that also provides the location of the resource. URLs for resources on the web contain three pieces of information. For this current page, the URL is composed of:</p><ul><li>The scheme used for determining the protocol used: <code>https</code></li><li>The authority or hostname of the server: <code>dart.dev</code></li><li>The path to the resource: <code>/tutorials/server/fetch-data.html</code></li></ul><p>There are other optional parameters as well that aren't used by the current page:</p><ul><li>Parameters to customize extra behavior: <code>?key1=value1&key2=value2</code></li><li>An anchor, that isn't sent to the server, which points to a specific location in the resource: <code>#uris</code></li></ul><p>To learn more about URLs, see <a href="https://developer.mozilla.org/docs/Learn/Common_questions/What_is_a_URL">What is a URL?</a> on the mdn web docs.</p><div class="header-wrapper"><h2 id="retrieve-the-necessary-dependencies">Retrieve the necessary dependencies</h2><a class="heading-link" href="#retrieve-the-necessary-dependencies" aria-label="Link to 'Retrieve the necessary dependencies' section">#</a></div><p>The <code>package:http</code> library provides a cross-platform solution for making composable HTTP requests, with optional fine-grained control.</p><aside class="alert alert-info"><div class="alert-header"><i class="material-symbols" aria-hidden="true">info</i> <span>Note</span></div><div class="alert-content"><p>You should avoid directly using <code>dart:io</code> or <code>dart:html</code> to make HTTP requests. Those libraries are platform-dependent and tied to a single implementation.</p></div></aside><p>To add a dependency on <code>package:http</code>, run the following <a href="/tools/pub/cmd/pub-add"><code>dart pub add</code></a> command from the top of your repo:</p><div class="code-block-wrapper language-console"><div class="code-block-body"><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">$ dart pub add http</span></span></code></pre></div></div><p>To use <code>package:http</code> in your code, import it and optionally <a href="/language/libraries#specifying-a-library-prefix">specify a library prefix</a>:</p> <?code-excerpt "lib/fetch_data.dart (http-import)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'package:http/http.dart'</span><span style="color:#D43324"> as</span><span style="color:#222222"> http;</span></span></code></pre></div></div><p>To learn more specifics about <code>package:http</code>, see its <a href="https://pub.dev/packages/http">page on the pub.dev site</a> and its <a href="https://pub.dev/documentation/http">API documentation</a>.</p><div class="header-wrapper"><h2 id="build-a-url">Build a URL</h2><a class="heading-link" href="#build-a-url" aria-label="Link to 'Build a URL' section">#</a></div><p>As previously mentioned, to make an HTTP request, you first need a URL that identifies the resource being requested or endpoint being accessed.</p><p>In Dart, URLs are represented through <a href="https://api.dart.dev/dart-core/Uri-class.html"><code>Uri</code></a> objects. There are many ways to build an <code>Uri</code>, but due to its flexibility, parsing a string with <code>Uri.parse</code> to create one is a common solution.</p><p>The following snippet shows two ways to create a <code>Uri</code> object pointing to mock JSON-formatted information about <code>package:http</code> hosted on this site:</p> <?code-excerpt "lib/fetch_data.dart (build-uris)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// Parse the entire URI, including the scheme</span></span> <span class="line"><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">parse</span><span style="color:#222222">(</span><span style="color:#11796D">'https://dart.dev/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Specifically create a URI with the https scheme</span></span> <span class="line"><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span></code></pre></div></div><p>To learn about other ways of building and interacting with URIs, see the <a href="/libraries/dart-core#uris"><code>URI</code> documentation</a>.</p><div class="header-wrapper"><h2 id="make-a-network-request">Make a network request</h2><a class="heading-link" href="#make-a-network-request" aria-label="Link to 'Make a network request' section">#</a></div><p>If you just need to quickly fetch a string representation of a requested resource, you can use the top-level <a href="https://pub.dev/documentation/http/latest/http/read.html"><code>read</code></a> function found in <code>package:http</code> that returns a <code>Future<String></code> or throws a <a href="https://pub.dev/documentation/http/latest/http/ClientException-class.html"><code>ClientException</code></a> if the request wasn't successful. The following example uses <code>read</code> to retrieve the mock JSON-formatted information about <code>package:http</code> as a string, then prints it out:</p><aside class="alert alert-info"><div class="alert-header"><i class="material-symbols" aria-hidden="true">info</i> <span>Note</span></div><div class="alert-content"><p>Many functions in <code>package:http</code>, including <code>read</code>, access the network and perform potentially time-consuming operations, therefore they do so asynchronously and return a <a href="https://api.dart.dev/dart-async/Future-class.html"><code>Future</code></a>. If you haven't encountered futures yet, you can learn about them鈥攁s well as the <code>async</code> and <code>await</code> keywords鈥攊n the <a href="/libraries/async/async-await">asynchronous programming tutorial</a>.</p></div></aside> <?code-excerpt "lib/fetch_data.dart (http-read)" replace="/readMain/main/g; /(http\.read)/[!$1!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageInfo = </span><span style="color:#D43324">await</span><span style="color:#222222"> </span><mark class="highlight"><span style="color:#222222">http.</span><span style="color:#6200EE">read</span></mark><span style="color:#222222">(httpPackageUrl);</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(httpPackageInfo);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>This results in the following JSON-formatted output, which can also be seen in your browser at <a href="/f/packages/http.json"><code>/f/packages/http.json</code></a>.</p><div class="code-block-wrapper language-json"><div class="code-block-body"><span class="code-block-language" title="Language json">json</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">{</span></span> <span class="line"><span style="color:#222222"> "</span><span style="color:#0468D7">name</span><span style="color:#222222">"</span><span style="color:#222222">: </span><span style="color:#11796D">"http"</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> "</span><span style="color:#0468D7">latestVersion</span><span style="color:#222222">"</span><span style="color:#222222">: </span><span style="color:#11796D">"1.1.2"</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> "</span><span style="color:#0468D7">description</span><span style="color:#222222">"</span><span style="color:#222222">: </span><span style="color:#11796D">"A composable, multi-platform, Future-based API for HTTP requests."</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> "</span><span style="color:#0468D7">publisher</span><span style="color:#222222">"</span><span style="color:#222222">: </span><span style="color:#11796D">"dart.dev"</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> "</span><span style="color:#0468D7">repository</span><span style="color:#222222">"</span><span style="color:#222222">: </span><span style="color:#11796D">"https://github.com/dart-lang/http"</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>Note the structure of the data (in this case a map), as you will need it when decoding the JSON later on.</p><p>If you need other information from the response, such as the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status">status code</a> or the <a href="https://developer.mozilla.org/docs/Web/HTTP/Headers">headers</a>, you can instead use the top-level <a href="https://pub.dev/documentation/http/latest/http/get.html"><code>get</code></a> function that returns a <code>Future</code> with a <a href="https://pub.dev/documentation/http/latest/http/Response-class.html"><code>Response</code></a>.</p><p>The following snippet uses <code>get</code> to get the whole response in order to exit early if the request was not successful, which is indicated with a status code of <strong>200</strong>:</p> <?code-excerpt "lib/fetch_data.dart (http-get)" replace="/getMain/main/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageResponse = </span><span style="color:#D43324">await</span><span style="color:#222222"> http.</span><span style="color:#6200EE">get</span><span style="color:#222222">(httpPackageUrl);</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (httpPackageResponse.statusCode != </span><span style="color:#11796D">200</span><span style="color:#222222">) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'Failed to retrieve the http package!'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(httpPackageResponse.body);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>There are many other status codes besides <strong>200</strong> and your app might want to handle them differently. To learn more about what different status codes mean, see <a href="https://developer.mozilla.org/docs/Web/HTTP/Status">HTTP response status codes</a> on the mdn web docs.</p><p>Some server requests require more information, such as authentication or user-agent information; in this case you might need to include <a href="https://developer.mozilla.org/docs/Web/HTTP/Headers">HTTP headers</a>. You can specify headers by passing in a <code>Map<String, String></code> of the key-value pairs as the <code>headers</code> optional named parameter:</p> <?code-excerpt "lib/fetch_data.dart (http-headers)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">await</span><span style="color:#222222"> http.</span><span style="color:#6200EE">get</span><span style="color:#222222">(</span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">),</span></span> <span class="line"><span style="color:#222222"> headers: {</span><span style="color:#11796D">'User-Agent'</span><span style="color:#222222">: </span><span style="color:#11796D">'<product name>/<product-version>'</span><span style="color:#222222">});</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="make-multiple-requests">Make multiple requests</h3><a class="heading-link" href="#make-multiple-requests" aria-label="Link to 'Make multiple requests' section">#</a></div><p>If you're making multiple requests to the same server, you can instead keep a persistent connection through a <a href="https://pub.dev/documentation/http/latest/http/Client-class.html"><code>Client</code></a>, which has similar methods to the top-level ones. Just make sure to clean up with the <a href="https://pub.dev/documentation/http/latest/http/Client/close.html"><code>close</code></a> method when done:</p> <?code-excerpt "lib/fetch_data.dart (http-client)" replace="/clientMain/main/g; /(http\.Cl.*?\))/[!$1!]/g; /(client\.c.*?\))/[!$1!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> client = </span><mark class="highlight"><span style="color:#222222">http.</span><span style="color:#0468D7">Client</span><span style="color:#222222">()</span></mark><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> try</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageInfo = </span><span style="color:#D43324">await</span><span style="color:#222222"> client.</span><span style="color:#6200EE">read</span><span style="color:#222222">(httpPackageUrl);</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(httpPackageInfo);</span></span> <span class="line"><span style="color:#222222"> } </span><span style="color:#D43324">finally</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> </span><mark class="highlight"><span style="color:#222222">client.</span><span style="color:#6200EE">close</span><span style="color:#222222">()</span></mark><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>To enable the client to retry failed requests, import <code>package:http/retry.dart</code> and wrap your created <code>Client</code> in a <a href="https://pub.dev/documentation/http/latest/retry/RetryClient-class.html"><code>RetryClient</code></a>:</p> <?code-excerpt "lib/fetch_data.dart (http-retry)" plaster="none" replace="/retryMain/main/g; /(i.*?retry.*)/[!$1!]/g; /(Retry.*?\)\))/[!$1!]/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'package:http/http.dart'</span><span style="color:#D43324"> as</span><span style="color:#222222"> http;</span></span> <span class="line"><mark class="highlight"><span style="color:#D43324">import</span><span style="color:#11796D"> 'package:http/retry.dart'</span><span style="color:#222222">;</span></mark></span> <span class="line"></span> <span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> client = </span><mark class="highlight"><span style="color:#0468D7">RetryClient</span><span style="color:#222222">(http.</span><span style="color:#0468D7">Client</span><span style="color:#222222">())</span></mark><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> try</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageInfo = </span><span style="color:#D43324">await</span><span style="color:#222222"> client.</span><span style="color:#6200EE">read</span><span style="color:#222222">(httpPackageUrl);</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(httpPackageInfo);</span></span> <span class="line"><span style="color:#222222"> } </span><span style="color:#D43324">finally</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> client.</span><span style="color:#6200EE">close</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>The <code>RetryClient</code> has a default behavior for how many times to retry and how long to wait between each request, but its behavior can be modified through parameters to the <a href="https://pub.dev/documentation/http/latest/retry/RetryClient/RetryClient.html"><code>RetryClient()</code></a> or <a href="https://pub.dev/documentation/http/latest/retry/RetryClient/RetryClient.withDelays.html"><code>RetryClient.withDelays()</code></a> constructors.</p><p><code>package:http</code> has much more functionality and customization, so make sure to check out its <a href="https://pub.dev/packages/http">page on the pub.dev site</a> and its <a href="https://pub.dev/documentation/http">API documentation</a>.</p><div class="header-wrapper"><h2 id="decode-the-retrieved-data">Decode the retrieved data</h2><a class="heading-link" href="#decode-the-retrieved-data" aria-label="Link to 'Decode the retrieved data' section">#</a></div><p>While you now have made a network request and retrieved the returned data as string, accessing specific portions of information from a string can be a challenge.</p><p>Since the data is already in a JSON format, you can use Dart's built-in <a href="https://api.dart.dev/dart-convert/JsonCodec/decode.html"><code>json.decode</code></a> function in the <code>dart:convert</code> library to convert the raw string into a JSON representation using Dart objects. In this case, the JSON data is represented in a map structure and, in JSON, map keys are always strings, so you can cast the result of <code>json.decode</code> to a <code>Map<String, dynamic></code>:</p> <?code-excerpt "lib/fetch_data.dart (json-decode)" plaster="none" replace="/decodeMain/main/g; /(import 'd.*?;)/[!$1!]/g; /(json\.de.*?)\;/[!$1!];/g"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><mark class="highlight"><span style="color:#D43324">import</span><span style="color:#11796D"> 'dart:convert'</span><span style="color:#222222">;</span></mark></span> <span class="line"></span> <span class="line"><span style="color:#D43324">import</span><span style="color:#11796D"> 'package:http/http.dart'</span><span style="color:#D43324"> as</span><span style="color:#222222"> http;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/http.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageInfo = </span><span style="color:#D43324">await</span><span style="color:#222222"> http.</span><span style="color:#6200EE">read</span><span style="color:#222222">(httpPackageUrl);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> httpPackageJson = </span><mark class="highlight"><span style="color:#222222">json.</span><span style="color:#6200EE">decode</span><span style="color:#222222">(httpPackageInfo) </span><span style="color:#D43324">as</span><span style="color:#0468D7"> Map</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">, </span><span style="color:#0468D7">dynamic</span><span style="color:#222222">></span></mark><span style="color:#222222">;</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(httpPackageJson);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="create-a-structured-class-to-store-the-data">Create a structured class to store the data</h3><a class="heading-link" href="#create-a-structured-class-to-store-the-data" aria-label="Link to 'Create a structured class to store the data' section">#</a></div><p>To provide the decoded JSON with more structure, making it easier to work with, create a class that can store the retrieved data using specific types depending on the schema of your data.</p><p>The following snippet shows a class-based representation that can store the package information returned from the mock JSON file you requested. This structure assumes all fields except the <code>repository</code> are required and provided every time.</p> <?code-excerpt "bin/fetch_http_package.dart (package-info)" plaster="none"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> name;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> latestVersion;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> description;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> publisher;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> Uri</span><span style="color:#222222">? repository;</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222">({</span></span> <span class="line"><span style="color:#D43324"> required</span><span style="color:#D43324"> this</span><span style="color:#222222">.name,</span></span> <span class="line"><span style="color:#D43324"> required</span><span style="color:#D43324"> this</span><span style="color:#222222">.latestVersion,</span></span> <span class="line"><span style="color:#D43324"> required</span><span style="color:#D43324"> this</span><span style="color:#222222">.description,</span></span> <span class="line"><span style="color:#D43324"> required</span><span style="color:#D43324"> this</span><span style="color:#222222">.publisher,</span></span> <span class="line"><span style="color:#D43324"> this</span><span style="color:#222222">.repository,</span></span> <span class="line"><span style="color:#222222"> });</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="encode-the-data-into-your-class">Encode the data into your class</h3><a class="heading-link" href="#encode-the-data-into-your-class" aria-label="Link to 'Encode the data into your class' section">#</a></div><p>Now that you have a class to store your data in, you need to add a mechanism to convert the decoded JSON into a <code>PackageInfo</code> object.</p><p>Convert the decoded JSON by manually writing a <code>fromJson</code> method matching the earlier JSON format, casting types as necessary and handling the optional <code>repository</code> field:</p> <?code-excerpt "bin/fetch_http_package.dart (from-json)"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6E6E70"> // 路路路</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> factory</span><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222">.</span><span style="color:#6200EE">fromJson</span><span style="color:#222222">(</span><span style="color:#0468D7">Map</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">, </span><span style="color:#0468D7">dynamic</span><span style="color:#222222">> json) {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> repository = json[</span><span style="color:#11796D">'repository'</span><span style="color:#222222">] </span><span style="color:#D43324">as</span><span style="color:#0468D7"> String</span><span style="color:#222222">?;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> name: json[</span><span style="color:#11796D">'name'</span><span style="color:#222222">] </span><span style="color:#D43324">as</span><span style="color:#0468D7"> String</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> latestVersion: json[</span><span style="color:#11796D">'latestVersion'</span><span style="color:#222222">] </span><span style="color:#D43324">as</span><span style="color:#0468D7"> String</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> description: json[</span><span style="color:#11796D">'description'</span><span style="color:#222222">] </span><span style="color:#D43324">as</span><span style="color:#0468D7"> String</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> publisher: json[</span><span style="color:#11796D">'publisher'</span><span style="color:#222222">] </span><span style="color:#D43324">as</span><span style="color:#0468D7"> String</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> repository: repository != </span><span style="color:#11796D">null</span><span style="color:#222222"> ? </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">tryParse</span><span style="color:#222222">(repository) : </span><span style="color:#11796D">null</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222"> );</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>A handwritten method, such as in the previous example, is often sufficient for relatively simple JSON structures, but there are more flexible options as well. To learn more about JSON serialization and deserialization, including automatic generation of the conversion logic, see the <a href="/guides/json">Using JSON</a> guide.</p><div class="header-wrapper"><h3 id="convert-the-response-to-an-object-of-your-structured-class">Convert the response to an object of your structured class</h3><a class="heading-link" href="#convert-the-response-to-an-object-of-your-structured-class" aria-label="Link to 'Convert the response to an object of your structured class' section">#</a></div><p>Now you have a class to store your data and a way to convert the decoded JSON object into an object of that type. Next, you can write a function that pulls everything together:</p><ol><li>Create your <code>URI</code> based off a passed-in package name.</li><li>Use <code>http.get</code> to retrieve the data for that package.</li><li>If the request didn't succeed, throw an <code>Exception</code> or preferably your own custom <code>Exception</code> subclass.</li><li>If the request succeeded, use <code>json.decode</code> to decode the response body into a JSON string.</li><li>Converted the decoded JSON string into a <code>PackageInfo</code> object using the <code>PackageInfo.fromJson</code> factory constructor you created.</li></ol> <?code-excerpt "bin/fetch_http_package.dart (get-package)" plaster="none"?> <div class="code-block-wrapper language-dart"><div class="code-block-body"><span class="code-block-language" title="Language dart">dart</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#0468D7">Future</span><span style="color:#222222"><</span><span style="color:#0468D7">PackageInfo</span><span style="color:#222222">> </span><span style="color:#6200EE">getPackage</span><span style="color:#222222">(</span><span style="color:#0468D7">String</span><span style="color:#222222"> packageName) </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> packageUrl = </span><span style="color:#0468D7">Uri</span><span style="color:#222222">.</span><span style="color:#6200EE">https</span><span style="color:#222222">(</span><span style="color:#11796D">'dart.dev'</span><span style="color:#222222">, </span><span style="color:#11796D">'/f/packages/</span><span style="color:#11796D">$</span><span style="color:#222222">packageName</span><span style="color:#11796D">.json'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> packageResponse = </span><span style="color:#D43324">await</span><span style="color:#222222"> http.</span><span style="color:#6200EE">get</span><span style="color:#222222">(packageUrl);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // If the request didn't succeed, throw an exception</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (packageResponse.statusCode != </span><span style="color:#11796D">200</span><span style="color:#222222">) {</span></span> <span class="line"><span style="color:#D43324"> throw</span><span style="color:#0468D7"> PackageRetrievalException</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> packageName: packageName,</span></span> <span class="line"><span style="color:#222222"> statusCode: packageResponse.statusCode,</span></span> <span class="line"><span style="color:#222222"> );</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#222222"> packageJson = json.</span><span style="color:#6200EE">decode</span><span style="color:#222222">(packageResponse.body) </span><span style="color:#D43324">as</span><span style="color:#0468D7"> Map</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">, </span><span style="color:#0468D7">dynamic</span><span style="color:#222222">>;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#0468D7"> PackageInfo</span><span style="color:#222222">.</span><span style="color:#6200EE">fromJson</span><span style="color:#222222">(packageJson);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> PackageRetrievalException</span><span style="color:#D43324"> implements</span><span style="color:#0468D7"> Exception</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> packageName;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> int</span><span style="color:#222222">? statusCode;</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7"> PackageRetrievalException</span><span style="color:#222222">({</span><span style="color:#D43324">required</span><span style="color:#D43324"> this</span><span style="color:#222222">.packageName, </span><span style="color:#D43324">this</span><span style="color:#222222">.statusCode});</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h2 id="utilize-the-converted-data">Utilize the converted data</h2><a class="heading-link" href="#utilize-the-converted-data" aria-label="Link to 'Utilize the converted data' section">#</a></div><p>Now that you've retrieved data and converted it to a more easily accessible format, you can use it however you'd like. Some possibilities include outputting information to a CLI, or displaying it in a <a href="/web">web</a> or <a href="https://flutter.dev">Flutter</a> app.</p><p>Here is complete, runnable example that requests, decodes, then displays the mock information about the <code>http</code> and <code>path</code> packages:</p> <?code-excerpt "bin/fetch_http_package.dart"?> <pre><code data-dartpad="true" data-embed="true" data-theme="light" title="Runnable Dart sample" data-run="false">import 'dart:convert'; import 'package:http/http.dart' as http; void main() async { await printPackageInformation('http'); print(''); await printPackageInformation('path'); } Future<void> printPackageInformation(String packageName) async { final PackageInfo packageInfo; try { packageInfo = await getPackage(packageName); } on PackageRetrievalException catch (e) { print(e); return; } print('Information about the $packageName package:'); print('Latest version: ${packageInfo.latestVersion}'); print('Description: ${packageInfo.description}'); print('Publisher: ${packageInfo.publisher}'); final repository = packageInfo.repository; if (repository != null) { print('Repository: $repository'); } } Future<PackageInfo> getPackage(String packageName) async { final packageUrl = Uri.https('dart.dev', '/f/packages/$packageName.json'); final packageResponse = await http.get(packageUrl); // If the request didn't succeed, throw an exception if (packageResponse.statusCode != 200) { throw PackageRetrievalException( packageName: packageName, statusCode: packageResponse.statusCode, ); } final packageJson = json.decode(packageResponse.body) as Map<String, dynamic>; return PackageInfo.fromJson(packageJson); } class PackageInfo { final String name; final String latestVersion; final String description; final String publisher; final Uri? repository; PackageInfo({ required this.name, required this.latestVersion, required this.description, required this.publisher, this.repository, }); factory PackageInfo.fromJson(Map<String, dynamic> json) { final repository = json['repository'] as String?; return PackageInfo( name: json['name'] as String, latestVersion: json['latestVersion'] as String, description: json['description'] as String, publisher: json['publisher'] as String, repository: repository != null ? Uri.tryParse(repository) : null, ); } } class PackageRetrievalException implements Exception { final String packageName; final int? statusCode; PackageRetrievalException({required this.packageName, this.statusCode}); @override String toString() { final buf = StringBuffer(); buf.write('Failed to retrieve package:$packageName information'); if (statusCode != null) { buf.write(' with a status code of $statusCode'); } buf.write('!'); return buf.toString(); } } </code></pre><aside class="alert alert-info"><div class="alert-header"><i class="material-symbols" aria-hidden="true">flutter</i> <span>Flutter note</span></div><div class="alert-content"><p>For another example that covers fetching then displaying data in Flutter, see the <a href="https://docs.flutter.dev/cookbook/networking/fetch-data">Fetching data from the internet</a> Flutter recipe.</p></div></aside><div class="header-wrapper"><h2 id="what-next">What next?</h2><a class="heading-link" href="#what-next" aria-label="Link to 'What next?' section">#</a></div><p>Now that you have retrieved, parsed, and used data from the internet, consider learning more about <a href="/language/concurrency">Concurrency in Dart</a>. If your data is large and complex, you can move retrieval and decoding to another <a href="/language/concurrency#isolates">isolate</a> as a background worker to prevent your interface from becoming unresponsive.</p><nav id="subnav"><ul><li class="previous"><a href="/tutorials/server/cmdline">⟨ Write command-line apps</a></li><li class="next"><a href="/tutorials/server/httpserver">Write HTTP servers ⟩</a></li></ul></nav><p id="page-github-links"><span>Unless stated otherwise, the documentation on this site reflects Dart 3.5.4. Page last updated on 2024-11-17.</span> <a href="https://github.com/dart-lang/site-www/tree/main/src/content/tutorials/server/fetch-data.md" target="_blank" rel="noopener">View source</a> <span>or </span><a href="https://github.com/dart-lang/site-www/issues/new?template=1_page_issue.yml&page-url=https://dart.dev/tutorials/server/fetch-data/&page-source=https://github.com/dart-lang/site-www/tree/main/src/content/tutorials/server/fetch-data.md" title="Report an issue with this page" target="_blank" rel="noopener">report an issue</a>.</p></div></article></main><footer id="page-footer"><div class="footer-section footer-main"><a href="/" class="brand" title="Dart"><img src="/assets/img/logo/logo-white-text.svg" alt="Dart" width="164"></a><div class="footer-social-links"><a href="https://medium.com/dartlang" target="_blank" rel="noopener" title="Medium blog"><svg><use href="/assets/img/social/medium.svg#medium"></use></svg> </a><a href="https://github.com/dart-lang" target="_blank" rel="noopener" title="GitHub"><svg><use href="/assets/img/social/github.svg#github"></use></svg> </a><a href="https://twitter.com/dart_lang" target="_blank" rel="noopener" title="X (Twitter)"><svg><use href="/assets/img/social/x.svg#x"></use></svg></a></div></div><div class="footer-section footer-tray"><div class="footer-licenses">Except as otherwise noted, this site is licensed under a <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>, and code samples are licensed under the <a href="https://opensource.org/licenses/BSD-3-Clause">3-Clause BSD License</a>.</div><div class="footer-utility-links"><ul><li><a href="/terms" title="Terms of use">Terms</a></li><li><a href="https://policies.google.com/privacy" target="_blank" rel="noopener" title="Privacy policy">Privacy</a></li><li><a href="/security" title="Security philosophy and practices">Security</a></li></ul></div></div></footer></body></html>