CINXE.COM
Learning Dart as a JavaScript developer | 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="Leverage your JavaScript knowledge when learning Dart."><title>Learning Dart as a JavaScript developer | 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="Learning Dart as a JavaScript developer"><meta name="twitter:description" content="Leverage your JavaScript knowledge when learning Dart."><meta property="og:title" content="Learning Dart as a JavaScript developer"><meta property="og:description" content="Leverage your JavaScript knowledge when learning Dart."><meta property="og:url" content="/resources/coming-from/js-to-dart/"><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>!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 highlight-languages"><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 collapsed collapsible" data-toggle="collapse" href="#sidenav-5" role="button" aria-expanded="false" aria-controls="sidenav-5">Development</a><ul class="nav flex-column flex-nowrap collapse" 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 collapsible collapsed" data-toggle="collapse" data-target="#sidenav-5-5" href="#sidenav-5-5" role="button" aria-expanded="false" aria-controls="sidenav-5-5">Command-line & server apps</a><ul class="nav flex-column flex-nowrap collapse" 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" 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 active collapsible" data-toggle="collapse" href="#sidenav-9" role="button" aria-expanded="true" aria-controls="sidenav-9">Resources</a><ul class="nav flex-column flex-nowrap collapse show" 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 active collapsible" data-toggle="collapse" data-target="#sidenav-9-6" href="#sidenav-9-6" role="button" aria-expanded="true" aria-controls="sidenav-9-6">Coming from ...</a><ul class="nav flex-column flex-nowrap collapse show" id="sidenav-9-6"><li class="nav-item"><a class="nav-link active" 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="#conventions-and-linting">Conventions and linting</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#built-in-types">Built-in types</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#primitive-types">Primitive Types</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#variables">Variables</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#final-and-const">Final and const</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#null-safety">Null safety</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#nullable-vs-non-nullable-types">Nullable vs non-nullable types</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#null-aware-operators">Null-aware operators</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#functions">Functions</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#arrow-syntax">Arrow syntax</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#parameters">Parameters</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#first-class-functions">First-class functions</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#anonymous-functions">Anonymous functions</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#generator-functions">Generator functions</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#statements">Statements</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#control-flow-ifelse-for-while-switch">Control flow (if/else, for, while, switch)</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#operators">Operators</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#assignment-operators">Assignment operators</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#cascades-operator">Cascades (.. operator)</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#collections">Collections</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#lists">Lists</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#sets">Sets</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#maps">Maps</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#unmodifiable">Unmodifiable</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#spread-operator">Spread operator</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#collection-iffor">Collection if/for</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#asynchrony">Asynchrony</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#futures">Futures</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#streams">Streams</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#classes">Classes</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#this-context">"this" context</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#constructors">Constructors</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#methods">Methods</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#extending-classes">Extending classes</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#classes-as-interfaces">Classes as interfaces</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#abstract-classes-and-methods">Abstract classes and methods</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#mixins">Mixins</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#extensions">Extensions</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#getters-and-setters">Getters and setters</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#public-and-private-members">Public and private members</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#late-variables">Late variables</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#generics">Generics</a><ul class="nav"><li class="toc-entry nav-item"><a class="nav-link" href="#generic-methods">Generic methods</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#generic-classes">Generic classes</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#restricting-generics">Restricting generics</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#generics-in-literals">Generics in literals</a></li></ul></li><li class="toc-entry nav-item"><a class="nav-link" href="#doc-comments">Doc comments</a></li><li class="toc-entry nav-item"><a class="nav-link" href="#next-steps">Next steps</a></li></ul></div><article><div class="content"><div id="site-content-title"><h1>Learning Dart as a JavaScript developer</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="#conventions-and-linting">Conventions and linting</a></li><li class="toc-entry"><a href="#built-in-types">Built-in types</a><ul><li class="toc-entry"><a href="#primitive-types">Primitive Types</a></li></ul></li><li class="toc-entry"><a href="#variables">Variables</a><ul><li class="toc-entry"><a href="#final-and-const">Final and const</a></li></ul></li><li class="toc-entry"><a href="#null-safety">Null safety</a><ul><li class="toc-entry"><a href="#nullable-vs-non-nullable-types">Nullable vs non-nullable types</a></li><li class="toc-entry"><a href="#null-aware-operators">Null-aware operators</a></li><li class="toc-entry"><a href="#functions">Functions</a></li><li class="toc-entry"><a href="#arrow-syntax">Arrow syntax</a></li><li class="toc-entry"><a href="#parameters">Parameters</a></li><li class="toc-entry"><a href="#first-class-functions">First-class functions</a></li><li class="toc-entry"><a href="#anonymous-functions">Anonymous functions</a></li><li class="toc-entry"><a href="#generator-functions">Generator functions</a></li></ul></li><li class="toc-entry"><a href="#statements">Statements</a><ul><li class="toc-entry"><a href="#control-flow-ifelse-for-while-switch">Control flow (if/else, for, while, switch)</a></li><li class="toc-entry"><a href="#operators">Operators</a></li><li class="toc-entry"><a href="#assignment-operators">Assignment operators</a></li><li class="toc-entry"><a href="#cascades-operator">Cascades (.. operator)</a></li></ul></li><li class="toc-entry"><a href="#collections">Collections</a><ul><li class="toc-entry"><a href="#lists">Lists</a></li><li class="toc-entry"><a href="#sets">Sets</a></li><li class="toc-entry"><a href="#maps">Maps</a></li><li class="toc-entry"><a href="#unmodifiable">Unmodifiable</a></li><li class="toc-entry"><a href="#spread-operator">Spread operator</a></li><li class="toc-entry"><a href="#collection-iffor">Collection if/for</a></li></ul></li><li class="toc-entry"><a href="#asynchrony">Asynchrony</a><ul><li class="toc-entry"><a href="#futures">Futures</a></li><li class="toc-entry"><a href="#streams">Streams</a></li></ul></li><li class="toc-entry"><a href="#classes">Classes</a><ul><li class="toc-entry"><a href="#this-context">"this" context</a></li><li class="toc-entry"><a href="#constructors">Constructors</a></li><li class="toc-entry"><a href="#methods">Methods</a></li><li class="toc-entry"><a href="#extending-classes">Extending classes</a></li><li class="toc-entry"><a href="#classes-as-interfaces">Classes as interfaces</a></li><li class="toc-entry"><a href="#abstract-classes-and-methods">Abstract classes and methods</a></li><li class="toc-entry"><a href="#mixins">Mixins</a></li><li class="toc-entry"><a href="#extensions">Extensions</a></li><li class="toc-entry"><a href="#getters-and-setters">Getters and setters</a></li><li class="toc-entry"><a href="#public-and-private-members">Public and private members</a></li><li class="toc-entry"><a href="#late-variables">Late variables</a></li></ul></li><li class="toc-entry"><a href="#generics">Generics</a><ul><li class="toc-entry"><a href="#generic-methods">Generic methods</a></li><li class="toc-entry"><a href="#generic-classes">Generic classes</a></li><li class="toc-entry"><a href="#restricting-generics">Restricting generics</a></li><li class="toc-entry"><a href="#generics-in-literals">Generics in literals</a></li></ul></li><li class="toc-entry"><a href="#doc-comments">Doc comments</a></li><li class="toc-entry"><a href="#next-steps">Next steps</a></li></ul><span class="site-toc--inline__toggle toc-toggle-more-items"><i class="material-symbols">more_horiz</i></span></div><p>This guide aims to leverage your JavaScript programming knowledge when learning Dart. It showcases key similarities and differences in both languages, and introduces Dart concepts that are unsupported in JavaScript. As a JavaScript developer, Dart should feel quite familiar, as both languages share many concepts.</p><p>Like JavaScript, Dart runs on an event loop, so both languages execute code in a similar way. For example, asynchronous concepts like futures (promises in JavaScript) and the <code>async/await</code> syntax are very similar.</p><p>Dart is strongly typed, unlike JavaScript. If you have used with TypeScript or Flow, this should simplify learning Dart. If you've mostly worked with pure JavaScript, it might be more of an adjustment. With strong typing, Dart catches many errors before compiling that might exist in JavaScript code.</p><p>Dart enables null safety by default. JavaScript doesn't support null safety. As a JavaScript developer, it might take a while to learn how to write null safe code, but the trade-off is better protection against null reference exceptions that are detected even before compiling Dart code. (Thereby avoiding those dreaded <code>TypeError</code>s that occur when doing operations on a JavaScript variable that turns out to be null.)</p><div class="header-wrapper"><h2 id="conventions-and-linting">Conventions and linting</h2><a class="heading-link" href="#conventions-and-linting" aria-label="Link to 'Conventions and linting' section">#</a></div><p>JavaScript and Dart both have linting tools to enforce standard conventions. While JavaScript offers many tools, standards, and configurations, Dart has one official set of layout and style conventions plus a linter to simplify compliance. The Dart analyzer lints code along with providing more analytical functions. To customize the lint rules for your project, follow the <a href="/tools/analysis">Customizing static analysis</a> instructions.</p><p>Dart provides <a href="/tools/dart-fix"><code>dart fix</code></a> to find and fix errors.</p><p>Dart also provides a code formatter similar to JavaScript tools like <a href="https://prettier.io/">Prettier</a>. To format code in any Dart project, run <a href="/tools/dart-format"><code>dart format</code></a> on your command line. The IDE plugins for Dart and Flutter also provide this ability.</p><p>Dart supports trailing commas for comma-separated lists of collections, parameters, or arguments. When you add the trailing comma, the formatter places each list item on its own line. When you believe your list may have more items at a later date, add the trailing comma. Avoid adding the trailing comma for the formatting benefit alone.</p><p>JavaScript supports trailing commas in list and map literals only.</p><aside class="alert alert-secondary"><div class="alert-header"><span>To learn more about:</span></div><div class="alert-content"><ul><li>Using commas to make your code read more like HTML, read <a href="https://docs.flutter.dev/development/tools/formatting#using-trailing-commas">Using trailing commas</a> on flutter.dev.</li><li>Linting Dart, read <a href="/tools/linter-rules">Linter rules</a>.</li><li>Writing good Dart code, read <a href="/effective-dart">Effective Dart</a>.</li></ul></div></aside><div class="header-wrapper"><h2 id="built-in-types">Built-in types</h2><a class="heading-link" href="#built-in-types" aria-label="Link to 'Built-in types' section">#</a></div><p>Both JavaScript and Dart categorize their data into <em>types</em>. Every variable has an associated type. The type determines the kind of value the variable can store and what operations can be performed on these values. Dart differs from JavaScript in that it assigns a static type to every expression and variable. The static type predicts the runtime type of the values of a variable, or of the value of an expression. This means that Dart apps have sound static typing.</p><p>JavaScript provides primitive types <code>num</code>, <code>string</code>, and <code>boolean</code> and the <code>null</code> value as well as <em>arrays</em> and a <code>Map</code> type.</p><p>Dart supports the following built-in types:</p><ul><li>Numbers (<code>num</code>, <code>int</code>, <code>double</code>)</li><li>Strings (<code>String</code>)</li><li>Booleans (<code>bool</code>)</li><li>Lists (<code>List</code>, also known as arrays)</li><li>Sets (<code>Set</code>)</li><li>Maps (<code>Map</code>)</li><li>Symbols (<code>Symbol</code>)</li><li>The value <code>null</code> (<code>Null</code>)</li></ul><p>To learn more, check out <a href="/language/built-in-types">Built-in types</a> in the <a href="/guides/language">Dart Language Tour</a>.</p><p>All non-<code>Null</code> types in Dart are subtypes of Object. All values are also objects. Dart doesn't use "primitive types" like JavaScript. By contrast, Dart normalizes or <em>canonicalizes</em> number, boolean and <code>null</code> values. This means only one <code>int</code> value with the numerical value <code>1</code> exists.</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>JavaScript has two equality operators, <code>==</code> and <code>===</code>. The <code>==</code> operator performs the equality test after doing any necessary type conversions on or to primitive values. The <code>===</code> operator doesn't perform type conversions. Dart uses the <code>identical</code> function to check if two values are the same object, and the <code>==</code> operator to check whether the objects consider themselves as equal.</p></div></aside><p>For example: The equals operator <code>==</code> and the <code>identical()</code> method return <code>true</code> for the same values of number types. Review the example shown in the following code:</p><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">var</span><span style="color:#222222"> a = </span><span style="color:#11796D">2</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> b = </span><span style="color:#11796D">1</span><span style="color:#222222"> + </span><span style="color:#11796D">1</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(a == b); </span><span style="color:#6E6E70">// Prints true</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#6200EE">identical</span><span style="color:#222222">(a, b)); </span><span style="color:#6E6E70">// Prints true; only one "2" object exists</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="primitive-types">Primitive Types</h3><a class="heading-link" href="#primitive-types" aria-label="Link to 'Primitive Types' section">#</a></div><p>This section covers how Dart represents primitive types from JavaScript.</p><div class="header-wrapper"><h4 id="numbers">Numbers</h4><a class="heading-link" href="#numbers" aria-label="Link to 'Numbers' section">#</a></div><p>Dart has three data types for holding numbers:</p><dl><dt><code>num</code></dt><dd>The equivalent to the generic number type in JavaScript.</dd><dt><code>int</code></dt><dd>A numeric value without a fractional part.</dd><dt><code>double</code></dt><dd>Any 64-bit (double-precision) floating point number.</dd></dl><p>The Dart API includes all these types as classes. Both the <code>int</code> and <code>double</code> types share <code>num</code> as their parent class:</p><p><img src="/assets/img/guides/number-classes.png" alt="num subclasses Object and int and double each subclass num"></p><p>As Dart considers numbers as objects, numbers can expose their own utility functions as object methods. You don't need to use an additional object to apply a function to a number.</p><p>For example, to round a <code>double</code> to an integer:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">let</span><span style="color:#222222"> rounded = Math.</span><span style="color:#6200EE">round</span><span style="color:#222222">(</span><span style="color:#11796D">2.5</span><span style="color:#222222">);</span></span></code></pre></div></div><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">var</span><span style="color:#222222"> rounded = </span><span style="color:#11796D">2.5</span><span style="color:#222222">.</span><span style="color:#6200EE">round</span><span style="color:#222222">();</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="strings">Strings</h4><a class="heading-link" href="#strings" aria-label="Link to 'Strings' section">#</a></div><p>Strings in Dart work like strings in JavaScript. To write a string literal, enclose it in single (<code>'</code>) or double (<code>"</code>) quotation marks. The majority of Dart developers use single quotes, but the language enforces no standard. Use double quotation marks if you don't want to escape single quotes within the string.</p><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">var</span><span style="color:#222222"> a = </span><span style="color:#11796D">'This is a string.'</span><span style="color:#222222">;</span></span></code></pre></div></div><div class="header-wrapper"><h5 id="escaping-special-characters">Escaping special characters</h5><a class="heading-link" href="#escaping-special-characters" aria-label="Link to 'Escaping special characters' section">#</a></div><p>To include a character with another meaning in a string, like a <code>$</code> used for string interpolation, you must escape that character. Escaping special characters in Dart works like JavaScript and most other languages. To escape special characters, precede that character with the backslash character (<code>\</code>).</p><p>The following code shows some examples.</p><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">final</span><span style="color:#222222"> singleQuotes = </span><span style="color:#11796D">'I</span><span style="color:#222222">\'</span><span style="color:#11796D">m learning Dart'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// I'm learning Dart</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> doubleQuotes = </span><span style="color:#11796D">"Escaping the </span><span style="color:#222222">\"</span><span style="color:#11796D"> character"</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Escaping the " character</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> dollarEscape = </span><span style="color:#11796D">'The price is </span><span style="color:#222222">\$</span><span style="color:#11796D">3.14.'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// The price is $3.14.</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> backslashEscape = </span><span style="color:#11796D">'The Dart string escape character is </span><span style="color:#222222">\\</span><span style="color:#11796D">.'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> unicode = </span><span style="color:#11796D">'</span><span style="color:#222222">\u</span><span style="color:#11796D">{1F60E}'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// 😎, Unicode scalar U+1F60E</span></span></code></pre></div></div><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 can use four-digit hexadecimal characters with or without curly braces. To learn more about working with unicode characters, see <a href="/language/built-in-types#runes-and-grapheme-clusters">Runes and grapheme clusters</a>.</p></div></aside><div class="header-wrapper"><h5 id="string-interpolation">String interpolation</h5><a class="heading-link" href="#string-interpolation" aria-label="Link to 'String interpolation' section">#</a></div><p>JavaScript supports template literals. These use backtick (<code>`</code>) character delimiters for the following reasons:</p><ul><li>To allow for multiline strings</li><li>To interpolate strings with embedded expressions</li><li>To create special constructs called tagged templates</li></ul><p>In Dart, you don't need to enclose a string in backticks to concatenate strings or use interpolations within string literals.</p><p>To learn more, check out <a href="/language/built-in-types#strings">Strings</a> in the Dart Language Tour.</p><p>As in JavaScript template literals, you can use the <code>${<expression>}</code> syntax to insert expressions into a string literal. Dart uses this syntax and allows you to omit the curly braces when the expression uses a single identifier.</p><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">var</span><span style="color:#222222"> food = </span><span style="color:#11796D">'bread'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> str = </span><span style="color:#11796D">'I eat </span><span style="color:#11796D">$</span><span style="color:#222222">food</span><span style="color:#11796D">'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// I eat bread</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> str = </span><span style="color:#11796D">'I eat </span><span style="color:#11796D">${</span><span style="color:#222222">food</span><span style="color:#11796D">}</span><span style="color:#11796D">'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// I eat bread</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="string-concatenation-and-multiline-declaration">String concatenation and multiline declaration</h4><a class="heading-link" href="#string-concatenation-and-multiline-declaration" aria-label="Link to 'String concatenation and multiline declaration' section">#</a></div><p>In JavaScript, you can define multiline strings using template literals. Dart has two ways to define multiline strings.</p><ol><li>Using implicit string concatenation: Dart concatenates any neighboring string literals, even when spread over multiple lines:<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">final</span><span style="color:#222222"> s1 = </span><span style="color:#11796D">'String '</span></span> <span class="line"><span style="color:#11796D"> 'concatenation'</span></span> <span class="line"><span style="color:#11796D"> " even works over line breaks."</span><span style="color:#222222">;</span></span></code></pre></div></div></li><li>Using a multi line string literal: When using three quotation marks (either single or double) on either side of the string, the literal can span multiple lines.<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">final</span><span style="color:#222222"> s2 = </span><span style="color:#11796D">'''</span></span> <span class="line"><span style="color:#11796D">You can create</span></span> <span class="line"><span style="color:#11796D">multiline strings like this one.</span></span> <span class="line"><span style="color:#11796D">'''</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> s3 = </span><span style="color:#11796D">"""</span></span> <span class="line"><span style="color:#11796D">This is also a</span></span> <span class="line"><span style="color:#11796D">multiline string."""</span><span style="color:#222222">;</span></span></code></pre></div></div></li></ol><div class="header-wrapper"><h4 id="equality">Equality</h4><a class="heading-link" href="#equality" aria-label="Link to 'Equality' section">#</a></div><p>Dart considers two strings equal when they contain the same sequence of code units. To determine if two strings have the same sequences, use the equal-to operator (<code>==</code>).</p><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">final</span><span style="color:#222222"> s1 = </span><span style="color:#11796D">'String '</span></span> <span class="line"><span style="color:#11796D"> 'concatenation'</span></span> <span class="line"><span style="color:#11796D"> " works even over line breaks."</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(s1 ==</span></span> <span class="line"><span style="color:#11796D"> 'String concatenation works even over '</span></span> <span class="line"><span style="color:#11796D"> 'line breaks.'</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="booleans">Booleans</h4><a class="heading-link" href="#booleans" aria-label="Link to 'Booleans' section">#</a></div><p>Boolean values in both Dart and Javascript express a binary condition. These two values represent whether a value or expression is <code>true</code> or <code>false</code>. You can return the values using the literals <code>true</code> and <code>false</code>, or produced them using expressions like <code>x < 5</code> or <code>y == null</code>.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">let</span><span style="color:#222222"> isBananaPeeled = </span><span style="color:#11796D">false</span><span style="color:#222222">;</span></span></code></pre></div></div><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">var</span><span style="color:#222222"> isBananaPeeled = </span><span style="color:#11796D">false</span><span style="color:#222222">;</span></span></code></pre></div></div><div class="header-wrapper"><h2 id="variables">Variables</h2><a class="heading-link" href="#variables" aria-label="Link to 'Variables' section">#</a></div><p>Variables in Dart work like variables in JavaScript, with two exceptions:</p><ol><li>Each variable has a type.</li><li>Dart scopes all variables at the block level, like <code>let</code> and <code>const</code> variables in JavaScript.</li></ol><p>A Dart variable gets its type in one of two ways:</p><ol><li>Declared: A type written in the declaration.</li><li>Inferred: An expression used to initialize the variable. By <a href="/effective-dart/design#dont-redundantly-type-annotate-initialized-local-variables">convention</a>, use <code>var</code> or <code>final</code> when the analyzer can infer the type.</li></ol><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// Declare and initialize a variable at once</span></span> <span class="line"><span style="color:#D43324">let</span><span style="color:#222222"> name = </span><span style="color:#11796D">"bob"</span><span style="color:#222222">;</span></span></code></pre></div></div><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">// Declare a variable with a specific type</span></span> <span class="line"><span style="color:#6E6E70">// when you don't provide an initial value</span></span> <span class="line"><span style="color:#0468D7">String</span><span style="color:#222222"> name;</span></span> <span class="line"><span style="color:#6E6E70">// Declare and initialize a variable</span></span> <span class="line"><span style="color:#6E6E70">// at the same time and Dart infers</span></span> <span class="line"><span style="color:#6E6E70">// the type</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> name = </span><span style="color:#11796D">'bob'</span><span style="color:#222222">;</span></span></code></pre></div></div><p>Variables can only accept values of their type.</p><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">var</span><span style="color:#222222"> name = </span><span style="color:#11796D">'bob'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">name = </span><span style="color:#11796D">5</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Forbidden, as `name` has type `String`.</span></span></code></pre></div></div><p>If you don't provide an initial value or explicit type, Dart infers the variable's type to be the catch-all type <code>dynamic</code>.</p><p>Like JavaScript variables, you can assign any value to Dart variables that use the <code>dynamic</code> type.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// Declare a variable</span></span> <span class="line"><span style="color:#D43324">let</span><span style="color:#222222"> name;</span></span> <span class="line"><span style="color:#6E6E70">// Initialize the variable</span></span> <span class="line"><span style="color:#222222">name = </span><span style="color:#11796D">"bob"</span><span style="color:#222222">;</span></span></code></pre></div></div><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">// Declare a variable without a type or assigned value</span></span> <span class="line"><span style="color:#6E6E70">// and Dart infers the 'dynamic' type</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> name;</span></span> <span class="line"><span style="color:#6E6E70">// Initialize the variable and the type remains `dynamic`</span></span> <span class="line"><span style="color:#222222">name = </span><span style="color:#11796D">'bob'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">name = </span><span style="color:#11796D">5</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Allowed, as `name` has type `dynamic`.</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="final-and-const">Final and const</h3><a class="heading-link" href="#final-and-const" aria-label="Link to 'Final and const' section">#</a></div><p>Both JavaScript and Dart use variable modifiers. Both use <code>const</code>, but differ in how <code>const</code> works. Where JavaScript would use <code>const</code>, Dart uses <code>final</code>.</p><p>When you add <code>final</code> to a Dart variable or <code>const</code> to a JavaScript variable, you must initialize the variable before other code can read its value. Once initialized, you can't change these variables' references.</p><p>When Dart uses <code>const</code>, it refers to special values that it creates when compiling. Dart uses limited expressions to create these immutable values. These expressions <em>cannot</em> have side effects. Under these conditions, the compiler can then predict the precise value of a constant variable or expression, not just its static type.</p><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">final</span><span style="color:#0468D7"> String</span><span style="color:#222222"> name;</span></span> <span class="line"><span style="color:#6E6E70">// Cannot read name here, not initialized.</span></span> <span class="line"><span style="color:#D43324">if</span><span style="color:#222222"> (useNickname) {</span></span> <span class="line"><span style="color:#222222"> name = </span><span style="color:#11796D">"Bob"</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">} </span><span style="color:#D43324">else</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> name = </span><span style="color:#11796D">"Robert"</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">(name); </span><span style="color:#6E6E70">// Properly initialized here.</span></span></code></pre></div></div><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>When you create an object, the class constructor must initialize the <code>final</code> instance variables. This ensures that these variables have a value before anyone can read them.</p><p>Learn more in the <a href="#classes">Classes</a> section.</p></div></aside><p>In Dart, <em>constant variables must contain constant values</em>. Non-constant variables can contain constant values that you can also mark as <code>const</code>.</p><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">var</span><span style="color:#222222"> foo = </span><span style="color:#D43324">const</span><span style="color:#222222"> [];</span></span> <span class="line"><span style="color:#6E6E70"> // foo is not constant, but the value it points to is.</span></span> <span class="line"><span style="color:#6E6E70"> // You can reassign foo to a different list value,</span></span> <span class="line"><span style="color:#6E6E70"> // but its current list value cannot be altered.</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> baz = []; </span><span style="color:#6E6E70">// Equivalent to `const []`</span></span></code></pre></div></div><p>Likewise, classes can have their own <code>const</code> constructors that produce immutable instances.</p><p>You can't modify a <code>const</code> variable in JavaScript or Dart. JavaScript does allow you to modify a <code>const</code> object's fields, but Dart does not.</p><p>To learn more, see the <a href="#classes">Classes</a> section.</p><div class="header-wrapper"><h2 id="null-safety">Null safety</h2><a class="heading-link" href="#null-safety" aria-label="Link to 'Null safety' section">#</a></div><p>Unlike JavaScript, Dart supports null safety. In Dart, all types default to non-nullable. This benefits Dart developers because Dart catches null reference exceptions when writing code, rather than at runtime.</p><div class="header-wrapper"><h3 id="nullable-vs-non-nullable-types">Nullable vs non-nullable types</h3><a class="heading-link" href="#nullable-vs-non-nullable-types" aria-label="Link to 'Nullable vs non-nullable types' section">#</a></div><p>None of the variables in the following code example can be <code>null</code>.</p><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">// In null-safe Dart, none of these can ever be null.</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> i = </span><span style="color:#11796D">42</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Inferred to be an int.</span></span> <span class="line"><span style="color:#0468D7">String</span><span style="color:#222222"> name = </span><span style="color:#6200EE">getFileName</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> b = </span><span style="color:#0468D7">Foo</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Foo() invokes a constructor</span></span></code></pre></div></div><p>To indicate that a variable might have the value <code>null</code>, add <code>?</code> to its type declaration:</p><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">int</span><span style="color:#222222">? aNullableInt = </span><span style="color:#11796D">null</span><span style="color:#222222">;</span></span></code></pre></div></div><p>The same goes for any other type declaration, such as a function declaration:</p><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">String</span><span style="color:#222222">? </span><span style="color:#6200EE">returnsNullable</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> random.</span><span style="color:#6200EE">nextDouble</span><span style="color:#222222">() < </span><span style="color:#11796D">0.5</span></span> <span class="line"><span style="color:#222222"> ? </span><span style="color:#11796D">'Sometimes null!'</span></span> <span class="line"><span style="color:#222222"> : </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> <span class="line"><span style="color:#0468D7">String</span><span style="color:#6200EE"> returnsNonNullable</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#11796D"> 'Never null!'</span><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="null-aware-operators">Null-aware operators</h3><a class="heading-link" href="#null-aware-operators" aria-label="Link to 'Null-aware operators' section">#</a></div><p>Dart supports several operators to deal with nullability. As in JavaScript, Dart supports the null assignment operator (<code>??=</code>), null-coalescing operator (<code>??</code>), and optional chaining operator (<code>?.</code>). These operators work the same as JavaScript.</p><div class="header-wrapper"><h4 id="operator">! Operator</h4><a class="heading-link" href="#operator" aria-label="Link to '! Operator' section">#</a></div><p>In cases where a nullable variable or expression might be non-null, you can tell the compiler to repress any compile time errors using the (<code>!</code>) operator. Place this operator after the expression.</p><p>Don't confuse this with Dart's not (<code>!</code>) operator, which uses the same symbol but place before the expression.</p><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">int</span><span style="color:#222222">? a = </span><span style="color:#11796D">5</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7">int</span><span style="color:#222222"> b = a; </span><span style="color:#6E6E70">// Not allowed.</span></span> <span class="line"><span style="color:#0468D7">int</span><span style="color:#222222"> b = a!; </span><span style="color:#6E6E70">// Allowed.</span></span></code></pre></div></div><p>At runtime, if a turns out to be <code>null</code>, a runtime error occurs.</p><p>Like the <code>?.</code> operator, use the <code>!</code> operator when accessing properties or methods on an object:</p><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:#222222">myObject!.someProperty;</span></span> <span class="line"><span style="color:#222222">myObject!.</span><span style="color:#6200EE">someMethod</span><span style="color:#222222">();</span></span></code></pre></div></div><p>If <code>myObject</code> is <code>null</code> at runtime, a runtime error occurs.</p><div class="header-wrapper"><h3 id="functions">Functions</h3><a class="heading-link" href="#functions" aria-label="Link to 'Functions' section">#</a></div><p>While Dart's functions work much the same as their counterparts in JavaScript, they do have some additional features, and some minor syntax differences when declaring them. Similar to JavaScript, you can declare functions pretty much anywhere, whether at the top level, as a class field, or in the local scope.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// On the top level</span></span> <span class="line"><span style="color:#D43324">function</span><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// As a class field</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Multiplier</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</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:#6E6E70">// In a local scope</span></span> <span class="line"><span style="color:#D43324">function</span><span style="color:#6200EE"> main</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> function</span><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(</span><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">4</span><span style="color:#222222">));</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">// On the top level</span></span> <span class="line"><span style="color:#0468D7">int</span><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// As a class field</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Multiplier</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</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:#6E6E70">// In a local scope</span></span> <span class="line"><span style="color:#6200EE">main</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">4</span><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="arrow-syntax">Arrow syntax</h3><a class="heading-link" href="#arrow-syntax" aria-label="Link to 'Arrow syntax' section">#</a></div><p>Both Dart and JavaScript support arrow syntax (<code>=></code>), but differ in how they support it. In Dart, you can only use the arrow syntax when the function contains a single expression or return statement.</p><p>For example, the following <code>isNoble</code> functions are equivalent:</p><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">bool</span><span style="color:#6200EE"> isNoble</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> atomicNumber) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> _nobleGases[atomicNumber] != </span><span style="color:#11796D">null</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">bool</span><span style="color:#6200EE"> isNoble</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> atomicNumber) => _nobleGases[atomicNumber] != </span><span style="color:#11796D">null</span><span style="color:#222222">;</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="parameters">Parameters</h3><a class="heading-link" href="#parameters" aria-label="Link to 'Parameters' section">#</a></div><p>In JavaScript, all parameters <em>can</em> be positional parameters. By default, Dart <em>requires</em> you to pass all parameters as arguments to functions.</p><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">int</span><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, </span><span style="color:#0468D7">int</span><span style="color:#222222"> b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#6200EE">main</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Valid. All parameters are provided.</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Invalid. All parameters must be provided.</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>This can change in two situations:</p><ol><li>The positional parameters are marked as optional.</li><li>The parameters are named and not marked as required.</li></ol><p>To define optional positional parameters, enclose them in square brackets following any required positional parameters. You can't follow optional parameters with required parameters.</p><p>Due to null safety, optional positional parameters must have a default value or be marked as nullable. To learn more, see the preceding section about <a href="#null-safety">null safety</a>.</p><p>The following code has one valid and two invalid examples of functions that define optional positional parameters.</p><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">// Valid: `b` has a default value of 5. `c` is marked as nullable.</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, [</span><span style="color:#0468D7">int</span><span style="color:#222222"> b = </span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#0468D7">int</span><span style="color:#222222">? c]) {</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:#6E6E70">// Invalid: a required positional parameter follows an optional one.</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, [</span><span style="color:#0468D7">int</span><span style="color:#222222"> b = </span><span style="color:#11796D">5</span><span style="color:#222222">], </span><span style="color:#0468D7">int</span><span style="color:#222222"> c) {</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:#6E6E70">// Invalid: Neither optional positional parameter has a default</span></span> <span class="line"><span style="color:#6E6E70">// value or has been flagged as nullable.</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, [</span><span style="color:#0468D7">int</span><span style="color:#222222"> b, </span><span style="color:#0468D7">int</span><span style="color:#222222"> c]) {</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 following example shows how to call a function with optional parameters:</p><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:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, [</span><span style="color:#0468D7">int</span><span style="color:#222222"> b = </span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#0468D7">int</span><span style="color:#222222">? c]) {</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:#6200EE">main</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#6E6E70"> // All are valid function calls.</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6200EE"> multiply</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">7</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>Dart supports <strong>named parameters</strong>. These don't have to be provided in the order they're defined, as with positional parameters. You refer to them by name instead. By default, these are optional, unless they're flagged as required. Named parameters are defined by surrounding them with curly braces. You can combine named parameters with required positional parameters—in this scenario, the named parameters are always placed after positional. When calling a function with named parameters, pass values by prefixing the passed value with the name of the parameter, separated by a colon. For example, <code>f(namedParameter: 5)</code>.</p><p>Again, with null safety, named parameters that are not flagged as required either need to have a default value or be flagged as nullable.</p><p>The following code defines a function with named parameters:</p><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">// Valid:</span></span> <span class="line"><span style="color:#6E6E70">// - `a` has been flagged as required</span></span> <span class="line"><span style="color:#6E6E70">// - `b` has a default value of 5</span></span> <span class="line"><span style="color:#6E6E70">// - `c` is marked as nullable</span></span> <span class="line"><span style="color:#6E6E70">// - Named parameters follow the positional one</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#0468D7">bool</span><span style="color:#222222"> x, {</span><span style="color:#D43324">required</span><span style="color:#0468D7"> int</span><span style="color:#222222"> a, </span><span style="color:#0468D7">int</span><span style="color:#222222"> b = </span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#0468D7">int</span><span style="color:#222222">? c}) {</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 following examples call a function with named parameters:</p><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">// All are valid function calls.</span></span> <span class="line"><span style="color:#6E6E70">// Beyond providing the required positional parameter:</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#11796D">false</span><span style="color:#222222">, a: </span><span style="color:#11796D">3</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Only provide required named parameters</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#11796D">false</span><span style="color:#222222">, a: </span><span style="color:#11796D">3</span><span style="color:#222222">, b: </span><span style="color:#11796D">9</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Override default value of `b`</span></span> <span class="line"><span style="color:#6200EE">multiply</span><span style="color:#222222">(</span><span style="color:#11796D">false</span><span style="color:#222222">, c: </span><span style="color:#11796D">9</span><span style="color:#222222">, a: </span><span style="color:#11796D">3</span><span style="color:#222222">, b: </span><span style="color:#11796D">2</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Provide all named parameters out of order</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="first-class-functions">First-class functions</h3><a class="heading-link" href="#first-class-functions" aria-label="Link to 'First-class functions' section">#</a></div><p>JavaScript and Dart treat functions as first-class citizens. This means that Dart treats functions as any other object. For example, the following code shows how to pass a function as a parameter to another function:</p><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"> printElement</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> element) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(element);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> list = [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">];</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Pass printElement as a parameter.</span></span> <span class="line"><span style="color:#222222">list.</span><span style="color:#6200EE">forEach</span><span style="color:#222222">(printElement);</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="anonymous-functions">Anonymous functions</h3><a class="heading-link" href="#anonymous-functions" aria-label="Link to 'Anonymous functions' section">#</a></div><p>JavaScript and Dart both support <a href="https://en.wikipedia.org/wiki/Anonymous_function"><em>anonymous</em> functions</a>, or functions without a name. As with named functions, you can pass anonymous functions like any other value. For example, store anonymous functions in a variable, pass them as an argument to another function, or return them from another function.</p><p>JavaScript has two ways to declare an anonymous function:</p><ol><li>Use a standard function expression</li><li>Use arrow syntax</li></ol><p>Likewise, Dart also has two ways to declare anonymous functions. Both work in a similar manner to the JavaScript arrow expression. Dart's anonymous functions do not support the extra functionality that comes with regular function expressions. For example, JavaScript's support for a function expression acting like a constructor, or creating a custom binding to this.</p><p>To learn more, see the <a href="#classes">Classes</a> section.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// A regular function expression</span></span> <span class="line"><span style="color:#6E6E70">// assigned to a variable</span></span> <span class="line"><span style="color:#D43324">let</span><span style="color:#6200EE"> funcExpr</span><span style="color:#222222"> = </span><span style="color:#D43324">function</span><span style="color:#222222">(a, b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#6E6E70">// The same anonymous function</span></span> <span class="line"><span style="color:#6E6E70">// expressed as an arrow</span></span> <span class="line"><span style="color:#6E6E70">// function with curly braces.</span></span> <span class="line"><span style="color:#D43324">let</span><span style="color:#6200EE"> arrowFuncExpr</span><span style="color:#222222"> = (a, b) </span><span style="color:#D43324">=></span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#6E6E70">// An arrow function with only</span></span> <span class="line"><span style="color:#6E6E70">// one return statement as</span></span> <span class="line"><span style="color:#6E6E70">// its contents does not</span></span> <span class="line"><span style="color:#6E6E70">// require a block.</span></span> <span class="line"><span style="color:#D43324">let</span><span style="color:#6200EE"> arrowFuncExpr2</span><span style="color:#222222"> = (a, b) </span><span style="color:#D43324">=></span><span style="color:#222222"> a * b;</span></span></code></pre></div></div><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">// Assign an anonymous function</span></span> <span class="line"><span style="color:#6E6E70">// to a variable.</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> blockFunc =</span></span> <span class="line"><span style="color:#222222"> optionalCallback ?? (</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, </span><span style="color:#0468D7">int</span><span style="color:#222222"> b) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> a * b;</span></span> <span class="line"><span style="color:#222222">};</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// For an expression with only a return statement,</span></span> <span class="line"><span style="color:#6E6E70">// you can use the arrow syntax:</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> singleFunc = (</span><span style="color:#0468D7">int</span><span style="color:#222222"> a, </span><span style="color:#0468D7">int</span><span style="color:#222222"> b) => a * b;</span></span></code></pre></div></div><p>As with JavaScript, you can pass anonymous functions to other functions. Developers often pass anonymous functions when using the <code>map</code> function for arrays and lists:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// returns [4, 5, 6]</span></span> <span class="line"><span style="color:#222222">[</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">].</span><span style="color:#6200EE">map</span><span style="color:#222222">(e </span><span style="color:#D43324">=></span><span style="color:#222222"> e + </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// returns [5, 7, 9]</span></span> <span class="line"><span style="color:#222222">[</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">].</span><span style="color:#6200EE">map</span><span style="color:#222222">(e </span><span style="color:#D43324">=></span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> e *= </span><span style="color:#11796D">2</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> e + </span><span style="color:#11796D">3</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">});</span></span></code></pre></div></div><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">// returns [4, 5, 6]</span></span> <span class="line"><span style="color:#222222">[</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">].</span><span style="color:#6200EE">map</span><span style="color:#222222">((e) => e + </span><span style="color:#11796D">3</span><span style="color:#222222">).</span><span style="color:#6200EE">toList</span><span style="color:#222222">();</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// returns [5, 7, 9]</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> list2 = [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">].</span><span style="color:#6200EE">map</span><span style="color:#222222">((e) {</span></span> <span class="line"><span style="color:#222222"> e *= </span><span style="color:#11796D">2</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> e + </span><span style="color:#11796D">3</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">}).</span><span style="color:#6200EE">toList</span><span style="color:#222222">();</span></span></code></pre></div></div><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>The <code>map</code> function in the previous examples returns an <code>Iterable<T></code>, rather than a <code>List<T></code>. The <code>toList</code> function converts the returned <code>Iterable</code> back to a <code>List</code>.</p><p>A list literal could achieve the same goal.</p><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">// These two statements are equivalent:</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">([</span><span style="color:#D43324">for</span><span style="color:#222222"> (</span><span style="color:#D43324">var</span><span style="color:#222222"> e </span><span style="color:#D43324">in</span><span style="color:#222222"> [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">]) e + </span><span style="color:#11796D">3</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">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">].</span><span style="color:#6200EE">map</span><span style="color:#222222">((e) => e + </span><span style="color:#11796D">3</span><span style="color:#222222">).</span><span style="color:#6200EE">toList</span><span style="color:#222222">());</span></span></code></pre></div></div></div></aside><div class="header-wrapper"><h3 id="generator-functions">Generator functions</h3><a class="heading-link" href="#generator-functions" aria-label="Link to 'Generator functions' section">#</a></div><p>Both languages support <a href="/language/functions#generators"><em>generator functions</em></a>. These functions return an iterable collection of items computed to avoid unnecessary work.</p><p>To write a generator function in Dart, add the <code>sync*</code> keyword after the function parameters, and return an <code>Iterable</code>. Add items to the final iterable using the <code>yield</code> keyword, or add whole sets of items using <code>yield*</code>.</p><p>The following example shows how to write a basic generator function:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">function*</span><span style="color:#6200EE"> naturalsTo</span><span style="color:#222222">(n) {</span></span> <span class="line"><span style="color:#D43324"> let</span><span style="color:#222222"> k = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> while</span><span style="color:#222222"> (k < n) {</span></span> <span class="line"><span style="color:#D43324"> yield</span><span style="color:#222222"> k++;</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:#6E6E70">// Returns [0, 1, 2, 3, 4]</span></span> <span class="line"><span style="color:#D43324">for</span><span style="color:#222222"> (</span><span style="color:#D43324">let</span><span style="color:#222222"> value of </span><span style="color:#6200EE">naturalsTo</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222">)) {</span></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(value);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">Iterable</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> </span><span style="color:#6200EE">naturalsTo</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> n) </span><span style="color:#D43324">sync</span><span style="color:#222222">* {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> k = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> while</span><span style="color:#222222"> (k < n) {</span></span> <span class="line"><span style="color:#D43324"> yield</span><span style="color:#222222"> k++;</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:#6E6E70">// Returns an iterable with [0, 1, 2, 3, 4]</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#6200EE">naturalsTo</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222">).</span><span style="color:#6200EE">toList</span><span style="color:#222222">());</span></span></code></pre></div></div><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">function*</span><span style="color:#6200EE"> doubleNaturalsTo</span><span style="color:#222222">(n) {</span></span> <span class="line"><span style="color:#D43324"> let</span><span style="color:#222222"> k = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> while</span><span style="color:#222222"> (k < n) {</span></span> <span class="line"><span style="color:#D43324"> yield*</span><span style="color:#222222"> [k, k];</span></span> <span class="line"><span style="color:#222222"> k++;</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:#6E6E70">// Returns [0, 0, 1, 1, 2, 2]</span></span> <span class="line"><span style="color:#D43324">for</span><span style="color:#222222"> (</span><span style="color:#D43324">let</span><span style="color:#222222"> value of </span><span style="color:#6200EE">doubleNaturalsTo</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">)) {</span></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(value);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">Iterable</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> </span><span style="color:#6200EE">doubleNaturalsTo</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> n) </span><span style="color:#D43324">sync</span><span style="color:#222222">* {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> k = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> while</span><span style="color:#222222"> (k < n) {</span></span> <span class="line"><span style="color:#D43324"> yield</span><span style="color:#222222">* [k, k];</span></span> <span class="line"><span style="color:#222222"> k++;</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:#6E6E70">// Returns an iterable with [0, 0, 1, 1, 2, 2]</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#6200EE">doubleNaturalsTo</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">));</span></span></code></pre></div></div><p>You can also define asynchronous generator functions, which return streams instead of iterables. Learn more in the upcoming <a href="#asynchrony">Asynchrony</a> section.</p><div class="header-wrapper"><h2 id="statements">Statements</h2><a class="heading-link" href="#statements" aria-label="Link to 'Statements' section">#</a></div><p>This section describes differences in statements between JavaScript and Dart.</p><div class="header-wrapper"><h3 id="control-flow-ifelse-for-while-switch">Control flow (if/else, for, while, switch)</h3><a class="heading-link" href="#control-flow-ifelse-for-while-switch" aria-label="Link to 'Control flow (if/else, for, while, switch)' section">#</a></div><p>Most control statements work like their JavaScript counterparts. Some have additional uses for <a href="#collections">Collections</a>.</p><div class="header-wrapper"><h4 id="iteration">Iteration</h4><a class="heading-link" href="#iteration" aria-label="Link to 'Iteration' section">#</a></div><p>While both JavaScript and Dart have <code>for-in</code> loops, their behaviors differ.</p><p>JavaScript's <code>for-in</code> loop iterates over an object's properties. To iterate over a JavaScript iterable object's elements, you must use <code>for-of</code> or <code>Array.forEach()</code>. Dart's <code>for-in</code> loop works like JavaScripts <code>for-of</code>.</p><p>The following example shows iterating over a collection and printing out each element:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">for</span><span style="color:#222222"> (</span><span style="color:#D43324">const</span><span style="color:#222222"> element of list) {</span></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(element);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">for</span><span style="color:#222222"> (</span><span style="color:#D43324">final</span><span style="color:#222222"> element </span><span style="color:#D43324">in</span><span style="color:#222222"> list) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(element);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="switch">Switch</h4><a class="heading-link" href="#switch" aria-label="Link to 'Switch' section">#</a></div><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>One key difference with the <code>switch</code> statement in JavaScript and Dart: when a case has no <code>break</code>, <code>continue</code>, or <code>return</code> statement, JavaScript allows execution to fall through and continue with the next statement. However, Dart only allows this when a case's body is empty.</p></div></aside><p>When using <code>continue</code> in a <code>switch</code> statement, you can combine it with a label that is put on a case:</p><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">switch</span><span style="color:#222222"> (testEnum) {</span></span> <span class="line"><span style="color:#D43324"> case</span><span style="color:#0468D7"> TestEnum</span><span style="color:#222222">.</span><span style="color:#0468D7">A</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">'A'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> continue</span><span style="color:#222222"> b;</span></span> <span class="line"><span style="color:#222222"> b:</span></span> <span class="line"><span style="color:#D43324"> case</span><span style="color:#0468D7"> TestEnum</span><span style="color:#222222">.</span><span style="color:#0468D7">B</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">'B'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> break</span><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="operators">Operators</h3><a class="heading-link" href="#operators" aria-label="Link to 'Operators' section">#</a></div><p>Both Dart and JavaScript contain predefined operators. Neither language supports adding new operators. Dart supports overloading some existing operators with the <code>operator</code> keyword. For example:</p><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"> Vector</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> x;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> y;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> z;</span></span> <span class="line"><span style="color:#0468D7"> Vector</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y, </span><span style="color:#D43324">this</span><span style="color:#222222">.z);</span></span> <span class="line"><span style="color:#0468D7"> Vector</span><span style="color:#D43324"> operator</span><span style="color:#222222"> +(</span><span style="color:#0468D7">Vector</span><span style="color:#222222"> other) => </span><span style="color:#0468D7">Vector</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> x + other.x, </span></span> <span class="line"><span style="color:#222222"> y + other.y,</span></span> <span class="line"><span style="color:#222222"> z + other.z,</span></span> <span class="line"><span style="color:#222222"> );</span></span> <span class="line"><span style="color:#0468D7"> Vector</span><span style="color:#D43324"> operator</span><span style="color:#222222"> *(</span><span style="color:#0468D7">double</span><span style="color:#222222"> scalar) => </span><span style="color:#0468D7">Vector</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> x * scalar,</span></span> <span class="line"><span style="color:#222222"> y * scalar,</span></span> <span class="line"><span style="color:#222222"> z * scalar,</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"><h4 id="arithmetic-operators">Arithmetic operators</h4><a class="heading-link" href="#arithmetic-operators" aria-label="Link to 'Arithmetic operators' section">#</a></div><p>The equality and relational operators of both languages are almost identical, as shown in the following table:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Meaning</th><th>JavaScript operator</th><th>Dart operator</th></tr></thead><tbody><tr><td>Add</td><td><code>+</code></td><td><code>+</code></td></tr><tr><td>Subtract</td><td><code>-</code></td><td><code>-</code></td></tr><tr><td>Unary minus, also known as negation</td><td><code>-expr</code></td><td><code>-expr</code></td></tr><tr><td>Multiply</td><td><code>*</code></td><td><code>*</code></td></tr><tr><td>Divide</td><td><code>/</code></td><td><code>/</code></td></tr><tr><td>Divide returning an integer result</td><td></td><td><code>~/</code></td></tr><tr><td>Get the remainder of an integer division (modulo)</td><td><code>%</code></td><td><code>%</code></td></tr><tr><td><code>x = x + 1</code> (expression value is <code>x + 1</code>)</td><td><code>++x</code></td><td><code>++x</code></td></tr><tr><td><code>x = x + 1</code> (expression value is <code>x</code>)</td><td><code>x++</code></td><td><code>x++</code></td></tr><tr><td><code>x = x - 1</code> (expression value is <code>x - 1</code>)</td><td><code>--x</code></td><td><code>--x</code></td></tr><tr><td><code>x = x - 1</code> (expression value is <code>x</code>)</td><td><code>x--</code></td><td><code>x--</code></td></tr></tbody></table></div><p>For example:</p><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">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> + </span><span style="color:#11796D">3</span><span style="color:#222222"> == </span><span style="color:#11796D">5</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> - </span><span style="color:#11796D">3</span><span style="color:#222222"> == -</span><span style="color:#11796D">1</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> * </span><span style="color:#11796D">3</span><span style="color:#222222"> == </span><span style="color:#11796D">6</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222"> / </span><span style="color:#11796D">2</span><span style="color:#222222"> == </span><span style="color:#11796D">2.5</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Result is a double</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222"> ~/ </span><span style="color:#11796D">2</span><span style="color:#222222"> == </span><span style="color:#11796D">2</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Result is an int</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222"> % </span><span style="color:#11796D">2</span><span style="color:#222222"> == </span><span style="color:#11796D">1</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Remainder</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">a = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">b = ++a; </span><span style="color:#6E6E70">// Increment a before b gets its value.</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(a == b); </span><span style="color:#6E6E70">// 1 == 1</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">a = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">b = a++; </span><span style="color:#6E6E70">// Increment a AFTER b gets its value.</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(a != b); </span><span style="color:#6E6E70">// 1 != 0</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">a = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">b = --a; </span><span style="color:#6E6E70">// Decrement a before b gets its value.</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(a == b); </span><span style="color:#6E6E70">// -1 == -1</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">a = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">b = a--; </span><span style="color:#6E6E70">// Decrement a AFTER b gets its value.</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(a != b); </span><span style="color:#6E6E70">// -1 != 0</span></span></code></pre></div></div><p>You've probably noticed that Dart also contains a <code>~/</code> operator (called a <em>truncating division operator</em>), that divides a double and outputs a floored integer:</p><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">assert</span><span style="color:#222222">(</span><span style="color:#11796D">25</span><span style="color:#222222"> == </span><span style="color:#11796D">50.4</span><span style="color:#222222"> ~/ </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">25</span><span style="color:#222222"> == </span><span style="color:#11796D">50.6</span><span style="color:#222222"> ~/ </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">25</span><span style="color:#222222"> == </span><span style="color:#11796D">51.6</span><span style="color:#222222"> ~/ </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="equality-and-relational-operators">Equality and relational operators</h4><a class="heading-link" href="#equality-and-relational-operators" aria-label="Link to 'Equality and relational operators' section">#</a></div><p>The equality and relational operators of both languages work in the same way:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Meaning</th><th>JavaScript operator</th><th>Dart operator</th></tr></thead><tbody><tr><td>Strict equal</td><td><code>===</code></td><td><code>==</code></td></tr><tr><td>Abstract equal</td><td><code>==</code></td><td></td></tr><tr><td>Strict not equal</td><td><code>!==</code></td><td><code>!=</code></td></tr><tr><td>Abstract not equal</td><td><code>!=</code></td><td></td></tr><tr><td>Greater than</td><td><code>></code></td><td><code>></code></td></tr><tr><td>Less than</td><td><code><</code></td><td><code><</code></td></tr><tr><td>Greater than or equal to</td><td><code>>=</code></td><td><code>>=</code></td></tr><tr><td>Less than or equal to</td><td><code><=</code></td><td><code><=</code></td></tr></tbody></table></div><p>The <code>==</code> and <code>!=</code> JavaScript operators have no equivalent.</p><p>For example:</p><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">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> == </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> != </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222"> > </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> < </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222"> >= </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222"> <= </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="type-test-operators">Type test operators</h4><a class="heading-link" href="#type-test-operators" aria-label="Link to 'Type test operators' section">#</a></div><p>The implementation of test operators is a bit different between the two languages:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Meaning</th><th>JavaScript operator</th><th>Dart operator</th></tr></thead><tbody><tr><td>Typecast</td><td></td><td><code>x as T</code></td></tr><tr><td>True if object has specified type</td><td><code>x instanceof T</code></td><td><code>x is T</code></td></tr><tr><td>True if object lacks specified type</td><td><code>!(x instanceof T)</code></td><td><code>x is! T</code></td></tr></tbody></table></div><p>The result of <code>obj is T</code> is true if <code>obj</code> implements the interface specified by <code>T</code>. For example, <code>obj is Object?</code> is always true.</p><p>Use the typecast operator (<code>as</code>) to ensure that a value has a particular type. The compiler can use that, if you <em>know</em> that the object will have that type.</p><p>For example:</p><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:#222222">(person </span><span style="color:#D43324">as</span><span style="color:#0468D7"> Employee</span><span style="color:#222222">).employeeNumber = </span><span style="color:#11796D">4204583</span><span style="color:#222222">;</span></span></code></pre></div></div><p>If you don't <em>know</em> that the object is of type <code>T</code>, then use <code>is T</code> to check the type <em>before</em> using the object.</p><p>In Dart, the types of local variables update within the scope of the if statement. This is not the case for instance variables.</p><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">if</span><span style="color:#222222"> (person is </span><span style="color:#0468D7">Employee</span><span style="color:#222222">) {</span></span> <span class="line"><span style="color:#222222"> person.employeeNumber = </span><span style="color:#11796D">4204583</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="logical-operators">Logical operators</h4><a class="heading-link" href="#logical-operators" aria-label="Link to 'Logical operators' section">#</a></div><p>You can invert or combine boolean expressions using logical operators. The logical operators of both languages are identical.</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Meaning</th><th>JavaScript operator</th><th>Dart operator</th></tr></thead><tbody><tr><td>Inverts next expression (changes false to true and vice versa)</td><td><code>!x</code></td><td><code>!x</code></td></tr><tr><td>Logical OR</td><td><code>||</code></td><td><code>||</code></td></tr><tr><td>Logical AND</td><td><code>&&</code></td><td><code>&&</code></td></tr></tbody></table></div><p>JavaScript allows any value to be used where you need a Boolean value. It then converts those values to either <code>true</code> or <code>false</code>. JavaScript considers empty strings and the number <code>0</code> to be "falsy" values. Dart allows <code>bool</code> values in conditions and as operands of logical operators.</p><p>For example:</p><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">if</span><span style="color:#222222"> (!done && (col == </span><span style="color:#11796D">0</span><span style="color:#222222"> || col == </span><span style="color:#11796D">3</span><span style="color:#222222">)) {</span></span> <span class="line"><span style="color:#6E6E70"> // ...Do something...</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="bitwise-and-shift-operators">Bitwise and shift operators</h4><a class="heading-link" href="#bitwise-and-shift-operators" aria-label="Link to 'Bitwise and shift operators' section">#</a></div><p>You can manipulate the individual bits of numbers by using bitwise and shift operators with integers. The operators of both languages are almost identical, as shown in the following table:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Meaning</th><th>JavaScript operator</th><th>Dart operator</th></tr></thead><tbody><tr><td>Bitwise AND</td><td><code>&</code></td><td><code>&</code></td></tr><tr><td>Bitwise OR</td><td><code>|</code></td><td><code>|</code></td></tr><tr><td>Bitwise XOR</td><td><code>^</code></td><td><code>^</code></td></tr><tr><td>Unary bitwise complement (0s become 1s; 1s become 0s)</td><td><code>~expr</code></td><td><code>~expr</code></td></tr><tr><td>Shift left</td><td><code><<</code></td><td><code><<</code></td></tr><tr><td>Shift right</td><td><code>>></code></td><td><code>>></code></td></tr><tr><td>Unsigned shift right</td><td><code>>>></code></td><td><code>>>></code></td></tr></tbody></table></div><p>For example:</p><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">final</span><span style="color:#222222"> value = </span><span style="color:#11796D">0x22</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> bitmask = </span><span style="color:#11796D">0x0f</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value & bitmask) == </span><span style="color:#11796D">0x02</span><span style="color:#222222">); </span><span style="color:#6E6E70">// AND</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value & ~bitmask) == </span><span style="color:#11796D">0x20</span><span style="color:#222222">); </span><span style="color:#6E6E70">// AND NOT</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value | bitmask) == </span><span style="color:#11796D">0x2f</span><span style="color:#222222">); </span><span style="color:#6E6E70">// OR</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value ^ bitmask) == </span><span style="color:#11796D">0x2d</span><span style="color:#222222">); </span><span style="color:#6E6E70">// XOR</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value << </span><span style="color:#11796D">4</span><span style="color:#222222">) == </span><span style="color:#11796D">0x220</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Shift left</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value >> </span><span style="color:#11796D">4</span><span style="color:#222222">) == </span><span style="color:#11796D">0x02</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Shift right</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((-value >> </span><span style="color:#11796D">4</span><span style="color:#222222">) == -</span><span style="color:#11796D">0x03</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Shift right</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((value >>> </span><span style="color:#11796D">4</span><span style="color:#222222">) == </span><span style="color:#11796D">0x02</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Unsigned shift right</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">((-value >>> </span><span style="color:#11796D">4</span><span style="color:#222222">) > </span><span style="color:#11796D">0</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Unsigned shift right</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="conditional-operator">Conditional operator</h4><a class="heading-link" href="#conditional-operator" aria-label="Link to 'Conditional operator' section">#</a></div><p>Both Dart and JavaScript contain a conditional operator (<code>?:</code>) for evaluating expressions. Some developers refer to this as a ternary operator because it takes three operands. As Dart has another operator (<code>[]=</code>) that takes three operands, call this operator (<code>?:</code>) the conditional operator. This operator works for expressions like <a href="/language/branches#if">if-else</a> does for statements.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">let</span><span style="color:#222222"> visibility = isPublic ? </span><span style="color:#11796D">"public"</span><span style="color:#222222"> : </span><span style="color:#11796D">"private"</span><span style="color:#222222">;</span></span></code></pre></div></div><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">final</span><span style="color:#222222"> visibility = isPublic ? </span><span style="color:#11796D">'public'</span><span style="color:#222222"> : </span><span style="color:#11796D">'private'</span><span style="color:#222222">;</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="assignment-operators">Assignment operators</h3><a class="heading-link" href="#assignment-operators" aria-label="Link to 'Assignment operators' section">#</a></div><p>Use the (<code>=</code>) operator to assign values.</p><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">// Assign value to a</span></span> <span class="line"><span style="color:#222222">a = value;</span></span></code></pre></div></div><p>This operator also has a null-aware variant (<code>??=</code>).</p><p>To learn more, see the <a href="#null-aware-operators">null-assignment</a> operator section.</p><p>JavaScript and Dart include operators that calculate and assign new values to the variable in the expression. These assignment operators use the right-side value and the variable initial value as operands.</p><p>The following table lists these assignment operators:</p><div class="table-wrapper"><table class="table table-striped"><thead><tr><th>Operator</th><th>Description</th></tr></thead><tbody><tr><td><code>=</code></td><td>Assignment</td></tr><tr><td><code>+=</code></td><td>Addition assignment</td></tr><tr><td><code>-=</code></td><td>Subtraction assignment</td></tr><tr><td><code>*=</code></td><td>Multiplication assignment</td></tr><tr><td><code>/=</code></td><td>Division assignment</td></tr><tr><td><code>~/=</code></td><td>Truncating division assignment</td></tr><tr><td><code>%=</code></td><td>Remainder (modulo) assignment</td></tr><tr><td><code>>>>=</code></td><td>Unsigned right shift assignment</td></tr><tr><td><code>^=</code></td><td>Bitwise XOR assignment</td></tr><tr><td><code><<=</code></td><td>Left shift assignment</td></tr><tr><td><code>>>=</code></td><td>Right shift assignment</td></tr><tr><td><code>&=</code></td><td>Bitwise AND assignment</td></tr><tr><td><code>|=</code></td><td>Bitwise OR assignment</td></tr></tbody></table></div><p>JavaScript does not support the <code>~/=</code> assignment operator.</p><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">var</span><span style="color:#222222"> a = </span><span style="color:#11796D">5</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">a *= </span><span style="color:#11796D">2</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Multiply `a` by 2 and assign the result back to a.</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(a); </span><span style="color:#6E6E70">// `a` is now 10.</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="cascades-operator">Cascades (<code>..</code> operator)</h3><a class="heading-link" href="#cascades-operator" aria-label="Link to 'Cascades (.. operator)' section">#</a></div><p>Dart allows you to chain multiple method calls, property assignments, or both on a single object. Dart refers to this as <em>cascading</em> and uses the cascade syntax (<code>..</code>) to perform this action.</p><p>JavaScript lacks this syntax.</p><p>The following example shows chaining multiple methods on a newly constructed object using the cascade syntax:</p><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">var</span><span style="color:#222222"> animal = </span><span style="color:#0468D7">Animal</span><span style="color:#222222">() </span><span style="color:#6E6E70">// Sets multiple properties and methods</span></span> <span class="line"><span style="color:#222222"> ..name = </span><span style="color:#11796D">"Bob"</span></span> <span class="line"><span style="color:#222222"> ..age = </span><span style="color:#11796D">5</span></span> <span class="line"><span style="color:#222222"> ..</span><span style="color:#6200EE">feed</span><span style="color:#222222">()</span></span> <span class="line"><span style="color:#222222"> ..</span><span style="color:#6200EE">walk</span><span style="color:#222222">();</span></span> <span class="line"></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(animal.name); </span><span style="color:#6E6E70">// "Bob"</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(animal.age); </span><span style="color:#6E6E70">// 5</span></span></code></pre></div></div><p>To make the first cascade syntax null-aware, write it as <code>?..</code>.</p><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">var</span><span style="color:#222222"> result = maybePerson</span></span> <span class="line"><span style="color:#222222"> ?..employment = employer</span></span> <span class="line"><span style="color:#222222"> ..salary = salary;</span></span></code></pre></div></div><p>Dart ignores the entire cascade if the <code>maybePerson</code> value is <code>null</code>.</p><div class="header-wrapper"><h2 id="collections">Collections</h2><a class="heading-link" href="#collections" aria-label="Link to 'Collections' section">#</a></div><p>This section covers some collection types in Dart and compare them to similar types in JavaScript.</p><div class="header-wrapper"><h3 id="lists">Lists</h3><a class="heading-link" href="#lists" aria-label="Link to 'Lists' section">#</a></div><p>Dart writes list literals in the same ways as JavaScript arrays. Dart encloses lists in square brackets and separate values with commas.</p><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">// Initialize list and specify full type</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#0468D7"> List</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">> list1 = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'one'</span><span style="color:#222222">, </span><span style="color:#11796D">'two'</span><span style="color:#222222">, </span><span style="color:#11796D">'three'</span><span style="color:#222222">];</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Initialize list using shorthand type</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> list2 = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'one'</span><span style="color:#222222">, </span><span style="color:#11796D">'two'</span><span style="color:#222222">, </span><span style="color:#11796D">'three'</span><span style="color:#222222">];</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Dart can also infer the type</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> list3 = [</span><span style="color:#11796D">'one'</span><span style="color:#222222">, </span><span style="color:#11796D">'two'</span><span style="color:#222222">, </span><span style="color:#11796D">'three'</span><span style="color:#222222">];</span></span></code></pre></div></div><p>The following code samples give an overview of the basic actions that you can perform on a Dart <code>List</code>. The following example shows how to retrieve a value from a <code>List</code> using the index operator.</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> fruit = fruits[</span><span style="color:#11796D">1</span><span style="color:#222222">];</span></span></code></pre></div></div><p>Add a value to the end of the <code>List</code> using the <code>add</code> method. Add another <code>List</code> using the <code>addAll</code> method:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">add</span><span style="color:#222222">(</span><span style="color:#11796D">'peach'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">addAll</span><span style="color:#222222">([</span><span style="color:#11796D">'kiwi'</span><span style="color:#222222">, </span><span style="color:#11796D">'mango'</span><span style="color:#222222">]);</span></span></code></pre></div></div><p>Insert a value at a specific position using the <code>insert</code> method. Insert another <code>List</code> at a specific position using the <code>insertAll</code> method:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">insert</span><span style="color:#222222">(</span><span style="color:#11796D">0</span><span style="color:#222222">, </span><span style="color:#11796D">'peach'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">insertAll</span><span style="color:#222222">(</span><span style="color:#11796D">0</span><span style="color:#222222">, [</span><span style="color:#11796D">'kiwi'</span><span style="color:#222222">, </span><span style="color:#11796D">'mango'</span><span style="color:#222222">]);</span></span></code></pre></div></div><p>Update a value in the <code>List</code> combining the index and assignment operators:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#222222">fruits[</span><span style="color:#11796D">2</span><span style="color:#222222">] = </span><span style="color:#11796D">'peach'</span><span style="color:#222222">;</span></span></code></pre></div></div><p>Remove items from a <code>List</code> using one of the following methods:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#6E6E70">// Remove the value 'pear' from the list.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">remove</span><span style="color:#222222">(</span><span style="color:#11796D">'pear'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6E6E70">// Removes the last element from the list.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeLast</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#6E6E70">// Removes the element at position 1 from the list.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeAt</span><span style="color:#222222">(</span><span style="color:#11796D">1</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6E6E70">// Removes the elements with positions greater than</span></span> <span class="line"><span style="color:#6E6E70">// or equal to start (1) and less than end (3) from the list.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeRange</span><span style="color:#222222">(</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6E6E70">// Removes all elements from the list that match the given predicate.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeWhere</span><span style="color:#222222">((fruit) => fruit.</span><span style="color:#6200EE">contains</span><span style="color:#222222">(</span><span style="color:#11796D">'p'</span><span style="color:#222222">));</span></span></code></pre></div></div><p>Use <code>length</code> to obtain the number of values in the <code>List</code>:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.length == </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span></code></pre></div></div><p>Use <code>isEmpty</code> to check if the <code>List</code> is empty:</p><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">var</span><span style="color:#222222"> fruits = [];</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.isEmpty);</span></span></code></pre></div></div><p>Use <code>isNotEmpty</code> to check if the <code>List</code> is not empty:</p><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">final</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>[</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.isNotEmpty);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="filled">Filled</h4><a class="heading-link" href="#filled" aria-label="Link to 'Filled' section">#</a></div><p>Dart's <code>List</code> class includes a way to create a List with each item having the same value. This <code>filled</code> constructor creates a fixed-length list of size <code>n</code> with one default value. The following example create a list of 3 items:</p><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">final</span><span style="color:#222222"> list1 = </span><span style="color:#0468D7">List</span><span style="color:#222222">.</span><span style="color:#6200EE">filled</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">'a'</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Creates: [ 'a', 'a', 'a' ]</span></span></code></pre></div></div><ul><li>You cannot add or remove elements from this list by default. To permit this list to add or remove elements, add <code>, growable: true</code> to the end of the parameter list.</li><li>You can access and update elements of this list using their index value.</li></ul><div class="header-wrapper"><h4 id="generate">Generate</h4><a class="heading-link" href="#generate" aria-label="Link to 'Generate' section">#</a></div><p>The Dart <code>List</code> class includes a way to create a List of incrementing values. This <code>generate</code> constructor creates a fixed-length list of size <code>n</code> with a template to build element values. This template takes the index as a parameter.</p><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">// Creates: [ 'a0', 'a1', 'a2' ]</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> list1 = </span><span style="color:#0468D7">List</span><span style="color:#222222">.</span><span style="color:#6200EE">generate</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, (index) => </span><span style="color:#11796D">'a</span><span style="color:#11796D">$</span><span style="color:#222222">index</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="sets">Sets</h3><a class="heading-link" href="#sets" aria-label="Link to 'Sets' section">#</a></div><p>Unlike JavaScript, Dart supports defining <code>Set</code>s with literals. Dart defines sets in the same way as lists, but using curly braces rather than square brackets. Sets are unordered collections that only contain unique items. Dart enforces the uniqueness of these items using hash codes, meaning that objects need hash values to be stored in a <code>Set</code>.</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>In Dart, the hash value defaults to the instance of an object but you can override it to use a set of properties. To learn more, see the <a href="https://api.dart.dev/dart-core/Object/hashCode.html"><code>hashCode</code></a> property page.</p></div></aside><p>The following code snippet shows how to initialize a <code>Set</code>:</p><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">final</span><span style="color:#222222"> abc = {</span><span style="color:#11796D">'a'</span><span style="color:#222222">, </span><span style="color:#11796D">'b'</span><span style="color:#222222">, </span><span style="color:#11796D">'c'</span><span style="color:#222222">};</span></span></code></pre></div></div><p>The syntax for creating an empty set might seem confusing at first, because specifying empty curly braces (<code>{}</code>) results in creating an empty <code>Map</code>. To create an empty <code>Set</code>, precede the <code>{}</code> declaration with a type argument or assign <code>{}</code> to a variable of type <code>Set</code>:</p><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">final</span><span style="color:#222222"> names = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>{};</span></span> <span class="line"><span style="color:#6E6E70">// Set<String> names = {}; // This works, too.</span></span> <span class="line"><span style="color:#6E6E70">// final names = {}; // Creates an empty map, not a set.</span></span></code></pre></div></div><p>The following examples provide an overview of the basic actions that you can perform on a Dart <code>Set</code>.</p><p>Add a value to the <code>Set</code> using the <code>add</code> method. Use the <code>addAll</code> method to add multiple values:</p><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">final</span><span style="color:#222222"> fruits = {</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">add</span><span style="color:#222222">(</span><span style="color:#11796D">'peach'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">addAll</span><span style="color:#222222">([</span><span style="color:#11796D">'kiwi'</span><span style="color:#222222">, </span><span style="color:#11796D">'mango'</span><span style="color:#222222">]);</span></span></code></pre></div></div><p>Use one of the following methods in <code>Set</code> to remove content from the set:</p><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">final</span><span style="color:#222222"> fruits = {</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#6E6E70">// Remove the value 'pear' from the set.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">remove</span><span style="color:#222222">(</span><span style="color:#11796D">'pear'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6E6E70">// Remove all elements in the supplied list from the set.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeAll</span><span style="color:#222222">([</span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'apple'</span><span style="color:#222222">]);</span></span> <span class="line"><span style="color:#6E6E70">// Removes all elements from the list that match the given predicate.</span></span> <span class="line"><span style="color:#222222">fruits.</span><span style="color:#6200EE">removeWhere</span><span style="color:#222222">((fruit) => fruit.</span><span style="color:#6200EE">contains</span><span style="color:#222222">(</span><span style="color:#11796D">'p'</span><span style="color:#222222">));</span></span></code></pre></div></div><p>Use <code>length</code> to get the number of values in the <code>Set</code>:</p><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">final</span><span style="color:#222222"> fruits = {</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.length == </span><span style="color:#11796D">3</span><span style="color:#222222">);</span></span></code></pre></div></div><p>Use <code>isEmpty</code> to check if the <code>Set</code> is empty:</p><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">var</span><span style="color:#222222"> fruits = <</span><span style="color:#0468D7">String</span><span style="color:#222222">>{};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.isEmpty);</span></span></code></pre></div></div><p>Use <code>isNotEmpty</code> to check if the <code>Set</code> is not empty:</p><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">final</span><span style="color:#222222"> fruits = {</span><span style="color:#11796D">'apple'</span><span style="color:#222222">, </span><span style="color:#11796D">'orange'</span><span style="color:#222222">, </span><span style="color:#11796D">'pear'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(fruits.isNotEmpty);</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="maps">Maps</h3><a class="heading-link" href="#maps" aria-label="Link to 'Maps' section">#</a></div><p>The <code>Map</code> type in Dart resembles the <code>Map</code> type in JavaScript. Both types associate keys with values. A key can be any object type if all keys have the same type. This rule applies to values as well. Each key occurs once at most, but you can use the same value multiple times.</p><p>Dart bases the dictionary on a hash table. This means that keys need to be hashable. Every Dart object contains a hash.</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>In Dart, the hash value of an object defaults to a value derived from the object's identity, and being compatible with an equality where the object can only equal itself. To introduce an equality based on the <em>contents</em> of the object, override <code>hashCode</code> and <code>operator==</code>.</p></div></aside><p>Consider these simple <code>Map</code> examples, created using literals:</p><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">final</span><span style="color:#222222"> gifts = {</span></span> <span class="line"><span style="color:#11796D"> 'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'second'</span><span style="color:#222222">: </span><span style="color:#11796D">'turtle doves'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'fifth'</span><span style="color:#222222">: </span><span style="color:#11796D">'golden rings'</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"> nobleGases = {</span></span> <span class="line"><span style="color:#11796D"> 2</span><span style="color:#222222">: </span><span style="color:#11796D">'helium'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 10</span><span style="color:#222222">: </span><span style="color:#11796D">'neon'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 18</span><span style="color:#222222">: </span><span style="color:#11796D">'argon'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222">};</span></span></code></pre></div></div><p>The following code samples provide an overview of the basic actions that you can perform on a Dart <code>Map</code>. The following example shows how to retrieve a value from a <code>Map</code> using the index operator.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> gift = gifts[</span><span style="color:#11796D">'first'</span><span style="color:#222222">];</span></span></code></pre></div></div><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>If the map does not include the lookup key, the index operator returns <code>null</code>.</p></div></aside><p>Use the <code>containsKey</code> method to check if the <code>Map</code> includes a key.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(gifts.</span><span style="color:#6200EE">containsKey</span><span style="color:#222222">(</span><span style="color:#11796D">'fifth'</span><span style="color:#222222">));</span></span></code></pre></div></div><p>Use the index assignment operator (<code>[]=</code>) to add or update an entry in the <code>Map</code>. If the <code>Map</code> doesn't yet contain the key, Dart adds the entry. If the key exists, Dart updates its value.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#222222">gifts[</span><span style="color:#11796D">'second'</span><span style="color:#222222">] = </span><span style="color:#11796D">'turtle'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Gets added</span></span> <span class="line"><span style="color:#222222">gifts[</span><span style="color:#11796D">'second'</span><span style="color:#222222">] = </span><span style="color:#11796D">'turtle doves'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Gets updated</span></span></code></pre></div></div><p>Use the <code>addAll</code> method to add another <code>Map</code>. Use the <code>addEntries</code> method to add other entries to the <code>Map</code>.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#222222">gifts[</span><span style="color:#11796D">'second'</span><span style="color:#222222">] = </span><span style="color:#11796D">'turtle doves'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">gifts.</span><span style="color:#6200EE">addAll</span><span style="color:#222222">({</span></span> <span class="line"><span style="color:#11796D"> 'second'</span><span style="color:#222222">: </span><span style="color:#11796D">'turtle doves'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'fifth'</span><span style="color:#222222">: </span><span style="color:#11796D">'golden rings'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222">});</span></span> <span class="line"><span style="color:#222222">gifts.</span><span style="color:#6200EE">addEntries</span><span style="color:#222222">([</span></span> <span class="line"><span style="color:#0468D7"> MapEntry</span><span style="color:#222222">(</span><span style="color:#11796D">'second'</span><span style="color:#222222">, </span><span style="color:#11796D">'turtle doves'</span><span style="color:#222222">),</span></span> <span class="line"><span style="color:#0468D7"> MapEntry</span><span style="color:#222222">(</span><span style="color:#11796D">'fifth'</span><span style="color:#222222">, </span><span style="color:#11796D">'golden rings'</span><span style="color:#222222">),</span></span> <span class="line"><span style="color:#222222">]);</span></span></code></pre></div></div><p>Use the <code>remove</code> method to remove an entry from the <code>Map</code>. Use the <code>removeWhere</code> method to remove all entries that satisfy a given test.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#222222">gifts.</span><span style="color:#6200EE">remove</span><span style="color:#222222">(</span><span style="color:#11796D">'first'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">gifts.</span><span style="color:#6200EE">removeWhere</span><span style="color:#222222">((key, value) => value == </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">);</span></span></code></pre></div></div><p>Use <code>length</code> to obtain the number of key-value pairs in the <code>Map</code>.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#222222">gifts[</span><span style="color:#11796D">'fourth'</span><span style="color:#222222">] = </span><span style="color:#11796D">'calling birds'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(gifts.length == </span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span></code></pre></div></div><p>Use <code>isEmpty</code> to check if the <code>Map</code> is empty.</p><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">final</span><span style="color:#222222"> gifts = {};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(gifts.isEmpty);</span></span></code></pre></div></div><p>Use <code>isNotEmpty</code> to check if the <code>Map</code> is not empty.</p><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">final</span><span style="color:#222222"> gifts = {</span><span style="color:#11796D">'first'</span><span style="color:#222222">: </span><span style="color:#11796D">'partridge'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">assert</span><span style="color:#222222">(gifts.isNotEmpty);</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="unmodifiable">Unmodifiable</h3><a class="heading-link" href="#unmodifiable" aria-label="Link to 'Unmodifiable' section">#</a></div><p>Pure JavaScript doesn't support immutability. Dart offers multiple ways to make collections like arrays, sets, or dictionaries immutable.</p><ul><li>If the collection is a compile-time constant and shouldn't be modified, use the <code>const</code> keyword:<br><code>const fruits = <String>{'apple', 'orange', 'pear'};</code></li><li>Assign the <code>Set</code> to a <code>final</code> field, meaning that the <code>Set</code> itself doesn't have to be a compile-time constant. This ensures that the field can't be overridden with another <code>Set</code>, but it still allows the size or the contents of the <code>Set</code> to be modified:<br><code>final fruits = <String>{'apple', 'orange', 'pear'};</code></li><li>Create a final version of your collection type using the <code>unmodifiable</code> constructor (as shown in the following example). This creates a collection that cannot change its size or content:</li></ul><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">final</span><span style="color:#222222"> _set = </span><span style="color:#0468D7">Set</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">>.</span><span style="color:#6200EE">unmodifiable</span><span style="color:#222222">([</span><span style="color:#11796D">'a'</span><span style="color:#222222">, </span><span style="color:#11796D">'b'</span><span style="color:#222222">, </span><span style="color:#11796D">'c'</span><span style="color:#222222">]);</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> _list = </span><span style="color:#0468D7">List</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">>.</span><span style="color:#6200EE">unmodifiable</span><span style="color:#222222">([</span><span style="color:#11796D">'a'</span><span style="color:#222222">, </span><span style="color:#11796D">'b'</span><span style="color:#222222">, </span><span style="color:#11796D">'c'</span><span style="color:#222222">]);</span></span> <span class="line"><span style="color:#D43324">final</span><span style="color:#222222"> _map = </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">String</span><span style="color:#222222">>.</span><span style="color:#6200EE">unmodifiable</span><span style="color:#222222">({</span><span style="color:#11796D">'foo'</span><span style="color:#222222">: </span><span style="color:#11796D">'bar'</span><span style="color:#222222">});</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="spread-operator">Spread operator</h3><a class="heading-link" href="#spread-operator" aria-label="Link to 'Spread operator' section">#</a></div><p>As in JavaScript, Dart supports embedding a list into another list using the spread operator (<code>...</code>) and the null-aware spread operator (<code>...?</code>).</p><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">var</span><span style="color:#222222"> list1 = [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> list2 = [</span><span style="color:#11796D">0</span><span style="color:#222222">, ...list1]; </span><span style="color:#6E6E70">// [0, 1, 2, 3]</span></span> <span class="line"><span style="color:#6E6E70">// When the list being inserted could be null:</span></span> <span class="line"><span style="color:#222222">list1 = </span><span style="color:#11796D">null</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> list2 = [</span><span style="color:#11796D">0</span><span style="color:#222222">, ...?list1]; </span><span style="color:#6E6E70">// [0]</span></span></code></pre></div></div><p>This also works for sets and maps:</p><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">// Spread operator with maps</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> map1 = {</span><span style="color:#11796D">'foo'</span><span style="color:#222222">: </span><span style="color:#11796D">'bar'</span><span style="color:#222222">, </span><span style="color:#11796D">'key'</span><span style="color:#222222">: </span><span style="color:#11796D">'value'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> map2 = {</span><span style="color:#11796D">'foo'</span><span style="color:#222222">: </span><span style="color:#11796D">'baz'</span><span style="color:#222222">, ...map1}; </span><span style="color:#6E6E70">// {foo: bar, key: value}</span></span> <span class="line"><span style="color:#6E6E70">// Spread operator with sets</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> set1 = {</span><span style="color:#11796D">'foo'</span><span style="color:#222222">, </span><span style="color:#11796D">'bar'</span><span style="color:#222222">};</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> set2 = {</span><span style="color:#11796D">'foo'</span><span style="color:#222222">, </span><span style="color:#11796D">'baz'</span><span style="color:#222222">, ...set1}; </span><span style="color:#6E6E70">// {foo, baz, bar}</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="collection-iffor">Collection if/for</h3><a class="heading-link" href="#collection-iffor" aria-label="Link to 'Collection if/for' section">#</a></div><p>In Dart, the <code>for</code> and <code>if</code> keywords have additional functionality when it comes to collections.</p><p>A collection <code>if</code> statement includes items from a list literal only when the specified condition is met:</p><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">var</span><span style="color:#222222"> nav = [</span></span> <span class="line"><span style="color:#11796D"> 'Home'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'Furniture'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#11796D"> 'Plants'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (promoActive) </span><span style="color:#11796D">'Outlet'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222">];</span></span></code></pre></div></div><p>It works similarly for maps and sets.</p><p>A collection <code>for</code> statement allows multiple items to be mapped into another list:</p><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">var</span><span style="color:#222222"> listOfInts = [</span><span style="color:#11796D">1</span><span style="color:#222222">, </span><span style="color:#11796D">2</span><span style="color:#222222">, </span><span style="color:#11796D">3</span><span style="color:#222222">];</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> listOfStrings = [</span></span> <span class="line"><span style="color:#11796D"> '#0'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#D43324"> for</span><span style="color:#222222"> (</span><span style="color:#D43324">var</span><span style="color:#222222"> i </span><span style="color:#D43324">in</span><span style="color:#222222"> listOfInts) </span><span style="color:#11796D">'#</span><span style="color:#11796D">$</span><span style="color:#222222">i</span><span style="color:#11796D">'</span><span style="color:#222222">,</span></span> <span class="line"><span style="color:#222222">]; </span><span style="color:#6E6E70">// [#0, #1, #2, #3]</span></span></code></pre></div></div><p>This also works in the same way for maps and sets.</p><div class="header-wrapper"><h2 id="asynchrony">Asynchrony</h2><a class="heading-link" href="#asynchrony" aria-label="Link to 'Asynchrony' section">#</a></div><p>Like JavaScript, the Dart Virtual Machine (VM) runs a single event loop that processes all your Dart code. This means that similar rules for asynchrony apply here. All of your code runs synchronously, but you can handle it in a different order, depending on how you use the asynchronous tools at your disposal. Here are some of these constructs and how they relate to their JavaScript counterparts.</p><div class="header-wrapper"><h3 id="futures">Futures</h3><a class="heading-link" href="#futures" aria-label="Link to 'Futures' section">#</a></div><p><code>Future</code> is Dart's version of a JavaScript <code>Promise</code>. Both are the <em>result</em> of an asynchronous operation that resolves at a later point.</p><p>Functions in Dart or in Dart packages might return a <code>Future</code>, rather than the value they represent, as the value might not be available until later.</p><p>The following example shows that handling a future works in the same way in Dart as a promise works in JavaScript.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> httpResponseBody = </span><span style="color:#6200EE">func</span><span style="color:#222222">();</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">httpResponseBody.</span><span style="color:#6200EE">then</span><span style="color:#222222">(value </span><span style="color:#D43324">=></span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#11796D"> `Promise resolved to a value: </span><span style="color:#222222">${</span><span style="color:#222222">value</span><span style="color:#222222">}</span><span style="color:#11796D">`</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="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">String</span><span style="color:#222222">> httpResponseBody = </span><span style="color:#6200EE">func</span><span style="color:#222222">();</span></span> <span class="line"></span> <span class="line"><span style="color:#222222">httpResponseBody.</span><span style="color:#6200EE">then</span><span style="color:#222222">((</span><span style="color:#0468D7">String</span><span style="color:#222222"> value) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'Future resolved to a value: </span><span style="color:#11796D">$</span><span style="color:#222222">value</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">});</span></span></code></pre></div></div><p>Similarly, futures can fail like promises. Catching errors works the same as well:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#222222">httpResponseBody</span></span> <span class="line"><span style="color:#222222"> .</span><span style="color:#6200EE">then</span><span style="color:#222222">(...)</span></span> <span class="line"><span style="color:#222222"> .</span><span style="color:#6200EE">catch</span><span style="color:#222222">(err </span><span style="color:#D43324">=></span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#11796D"> "Promise encountered an error before resolving."</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="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:#222222">httpResponseBody</span></span> <span class="line"><span style="color:#222222"> .</span><span style="color:#6200EE">then</span><span style="color:#222222">(...)</span></span> <span class="line"><span style="color:#222222"> .</span><span style="color:#6200EE">catchError</span><span style="color:#222222">((err) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#11796D"> 'Future encountered an error before resolving.'</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>You can also create futures. To create a <code>Future</code>, define and call an <code>async</code> function. When you have a value that needs to be a <code>Future</code>, convert the function as in the following example.</p><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">String</span><span style="color:#222222"> str = </span><span style="color:#11796D">'String Value'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#0468D7">Future</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">> strFuture = </span><span style="color:#0468D7">Future</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">>.</span><span style="color:#6200EE">value</span><span style="color:#222222">(str);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="asyncawait">Async/Await</h4><a class="heading-link" href="#asyncawait" aria-label="Link to 'Async/Await' section">#</a></div><p>If you're familiar with promises in JavaScript, you're likely also familiar with the <code>async</code>/<code>await</code> syntax. This syntax is identical in Dart: functions are marked <code>async</code>, and <code>async</code> functions always return a <code>Future</code>. If the function returns a <code>String</code> and is marked <code>async</code>, it returns a <code>Future<String></code> instead. If it returns nothing, but it is <code>async</code>, it returns <code>Future<void></code>.</p><p>The following example shows how to write an <code>async</code> function:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#6E6E70">// Returns a Promise of a string,</span></span> <span class="line"><span style="color:#6E6E70">// as the method is async</span></span> <span class="line"><span style="color:#222222">async </span><span style="color:#6200EE">fetchString</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#6E6E70"> // Typically some other async</span></span> <span class="line"><span style="color:#6E6E70"> // operations would be done here.</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#11796D"> "String Value"</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><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">// Returns a future of a string,</span></span> <span class="line"><span style="color:#6E6E70">// as the method is async</span></span> <span class="line"><span style="color:#0468D7">Future</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">> </span><span style="color:#6200EE">fetchString</span><span style="color:#222222">() </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6E6E70"> // Typically some other async</span></span> <span class="line"><span style="color:#6E6E70"> // operations would be done here.</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#11796D"> 'String Value'</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>Call this <code>async</code> function as follows:</p><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">String</span><span style="color:#222222">> stringFuture = </span><span style="color:#6200EE">fetchString</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">stringFuture.</span><span style="color:#6200EE">then</span><span style="color:#222222">((</span><span style="color:#0468D7">String</span><span style="color:#222222"> str) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(str); </span><span style="color:#6E6E70">// 'String Value'</span></span> <span class="line"><span style="color:#222222">});</span></span></code></pre></div></div><p>Obtain a future's value using the <code>await</code> keyword. As in JavaScript, this removes the need to call <code>then</code> on the <code>Future</code> to obtain its value, and it allows you to write asynchronous code in a more synchronous-like way. As in JavaScript, awaiting futures is only possible within an <code>async</code> context (such as another <code>async</code> function).</p><p>The following example shows how to await a future for its value:</p><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">// We can only await futures within an async context.</span></span> <span class="line"><span style="color:#0468D7">Future</span><span style="color:#222222"><</span><span style="color:#D43324">void</span><span style="color:#222222">> </span><span style="color:#6200EE">asyncFunction</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"> var</span><span style="color:#222222"> str = </span><span style="color:#D43324">await</span><span style="color:#6200EE"> fetchString</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(str); </span><span style="color:#6E6E70">// 'String Value'</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>To learn more about <code>Future</code>s and the <code>async</code>/<code>await</code> syntax, see the <a href="/libraries/async/async-await">Asynchronous programming</a> tutorial.</p><div class="header-wrapper"><h3 id="streams">Streams</h3><a class="heading-link" href="#streams" aria-label="Link to 'Streams' section">#</a></div><p>Another tool in Dart's async toolbox is <code>Stream</code>s. While JavaScript has its own concept of streams, Dart's are more akin to <code>Observable</code>s, as found in the commonly used <code>rxjs</code> library. If you happen to be familiar with this library, Dart's streams should feel familiar.</p><p>For those not familiar with these concepts: <code>Stream</code>s basically act like <code>Future</code>s, but with multiple values spread out over time, like an event bus. Your code can listen to a stream, and it can either complete or reach a fail state.</p><div class="header-wrapper"><h4 id="listening">Listening</h4><a class="heading-link" href="#listening" aria-label="Link to 'Listening' section">#</a></div><p>To listen to a stream, call its <code>listen</code> method and provide a callback method. Whenever the stream emits a value, Dart calls this method:</p><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">Stream</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> stream = ...</span></span> <span class="line"><span style="color:#222222">stream.</span><span style="color:#6200EE">listen</span><span style="color:#222222">((</span><span style="color:#0468D7">int</span><span style="color:#222222"> value) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'A value has been emitted: </span><span style="color:#11796D">$</span><span style="color:#222222">value</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">});</span></span></code></pre></div></div><p>The <code>listen</code> method includes optional callbacks for handling errors or for when the stream completes:</p><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:#222222">stream.</span><span style="color:#6200EE">listen</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> (</span><span style="color:#0468D7">int</span><span style="color:#222222"> value) { ... },</span></span> <span class="line"><span style="color:#222222"> onError: (err) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'Stream encountered an error! </span><span style="color:#11796D">$</span><span style="color:#222222">err</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222"> },</span></span> <span class="line"><span style="color:#222222"> onDone: () {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'Stream completed!'</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>listen</code> method returns an instance of a <code>StreamSubscription</code>, which you can use to stop listening to the stream:</p><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">StreamSubscription</span><span style="color:#222222"> subscription = stream.</span><span style="color:#6200EE">listen</span><span style="color:#222222">(...);</span></span> <span class="line"><span style="color:#222222">subscription.</span><span style="color:#6200EE">cancel</span><span style="color:#222222">();</span></span></code></pre></div></div><p>This is not the only way to listen to a stream. Similar to the <code>async</code>/<code>await</code> syntax for <code>Future</code>s, you can combine a stream with a <code>for-in</code> loop in an <code>async</code> context. The <code>for</code> loop invokes the callback method for each item emitted, and it ends when the stream completes or errors out:</p><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">int</span><span style="color:#222222">> </span><span style="color:#6200EE">sumStream</span><span style="color:#222222">(</span><span style="color:#0468D7">Stream</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> stream) </span><span style="color:#D43324">async</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> var</span><span style="color:#222222"> sum = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> await</span><span style="color:#D43324"> for</span><span style="color:#222222"> (</span><span style="color:#D43324">final</span><span style="color:#222222"> value </span><span style="color:#D43324">in</span><span style="color:#222222"> stream) {</span></span> <span class="line"><span style="color:#222222"> sum += value;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> sum;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>When an error occurs when listening to a stream in this way, the error is rethrown at the line containing the <code>await</code> keyword. You can handle this error with a <code>try-catch</code> statement:</p><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">try</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> await</span><span style="color:#D43324"> for</span><span style="color:#222222"> (</span><span style="color:#D43324">final</span><span style="color:#222222"> value </span><span style="color:#D43324">in</span><span style="color:#222222"> stream) { ... }</span></span> <span class="line"><span style="color:#222222">} </span><span style="color:#D43324">catch</span><span style="color:#222222"> (err) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'Stream encountered an error! </span><span style="color:#11796D">$</span><span style="color:#222222">err</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="creating-streams">Creating streams</h4><a class="heading-link" href="#creating-streams" aria-label="Link to 'Creating streams' section">#</a></div><p>As with <code>Future</code>s, you have several different ways to create a stream. The <code>Stream</code> class has utility constructors for creating streams from <code>Future</code>s or <code>Iterable</code>s, or for creating streams that emit values at a timed interval. To learn more, see the <a href="https://api.dart.dev/dart-async/Stream-class.html"><code>Stream</code></a> API page.</p><div class="header-wrapper"><h5 id="streamcontroller">StreamController</h5><a class="heading-link" href="#streamcontroller" aria-label="Link to 'StreamController' section">#</a></div><p>The utility class <a href="https://api.dart.dev/dart-async/StreamController-class.html"><code>StreamController</code></a> can create and control streams. Its stream property exposes the stream it controls. Its methods provides ways to add events to that stream.</p><p>For example, the <code>add</code> method can emit new items and the <code>close</code> method completes the stream.</p><p>The following example shows basic usage of a stream controller:</p><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">var</span><span style="color:#222222"> listeners = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#0468D7">StreamController</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">>? controller;</span></span> <span class="line"><span style="color:#222222">controller = </span><span style="color:#0468D7">StreamController</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">>(</span></span> <span class="line"><span style="color:#222222"> onListen: () {</span></span> <span class="line"><span style="color:#6E6E70"> // Emit a new value every time the stream gets a new listener.</span></span> <span class="line"><span style="color:#222222"> controller!.</span><span style="color:#6200EE">add</span><span style="color:#222222">(listeners++);</span></span> <span class="line"><span style="color:#6E6E70"> // Close the stream after the fifth listener.</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (listeners > </span><span style="color:#11796D">5</span><span style="color:#222222">) controller.</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> <span class="line"><span style="color:#6E6E70">// Get the stream for the stream controller</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> stream = controller.stream;</span></span> <span class="line"><span style="color:#6E6E70">// Listen to the stream</span></span> <span class="line"><span style="color:#222222">stream.</span><span style="color:#6200EE">listen</span><span style="color:#222222">((</span><span style="color:#0468D7">int</span><span style="color:#222222"> value) {</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#11796D">'</span><span style="color:#11796D">$</span><span style="color:#222222">value</span><span style="color:#11796D">'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">});</span></span></code></pre></div></div><div class="header-wrapper"><h5 id="async-generators">Async generators</h5><a class="heading-link" href="#async-generators" aria-label="Link to 'Async generators' section">#</a></div><p>Async generator functions can create streams. These functions resemble a synchronous generator function but use the <code>async*</code> keyword and return a <code>Stream</code>.</p><p>In an async generator function, the <code>yield</code> keyword emits the given value to the stream. The <code>yield*</code> keyword, however, works with streams instead of other iterables. This allows events from other streams to be emitted to this stream. In the following example, the function continues once the newly yielded stream has completed.</p><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">Stream</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> </span><span style="color:#6200EE">asynchronousNaturalsTo</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> n) </span><span style="color:#D43324">async</span><span style="color:#222222">* {</span></span> <span class="line"><span style="color:#D43324"> var</span><span style="color:#222222"> k = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324"> while</span><span style="color:#222222"> (k < n) </span><span style="color:#D43324">yield</span><span style="color:#222222"> k++;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7">Stream</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">> stream = </span><span style="color:#6200EE">asynchronousNaturalsTo</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222">);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Prints each of 0 1 2 3 4 in succession.</span></span> <span class="line"><span style="color:#222222">stream.</span><span style="color:#6200EE">forEach</span><span style="color:#222222">(</span><span style="color:#6200EE">print</span><span style="color:#222222">(value));</span></span></code></pre></div></div><p>Learn more about futures, streams, and other asynchronous functionality in the <a href="/libraries/async/async-await">asynchronous programming</a> docs.</p><div class="header-wrapper"><h2 id="classes">Classes</h2><a class="heading-link" href="#classes" aria-label="Link to 'Classes' section">#</a></div><p>On the surface, classes in Dart are similar to classes in JavaScript, although JavaScript classes are technically more of a wrapper around prototypes. In Dart, classes are a standard feature of the language. This section covers defining and using classes in Dart and how they differ from JavaScript.</p><div class="header-wrapper"><h3 id="this-context">"this" context</h3><a class="heading-link" href="#this-context" aria-label="Link to '"this" context' section">#</a></div><p>The <code>this</code> keyword in Dart is more straightforward than in JavaScript. In Dart, you can't bind functions to <code>this</code>, and <code>this</code> never depends on the execution context (as it does in JavaScript). In Dart, <code>this</code> is only used within classes, and always refers to the current instance.</p><div class="header-wrapper"><h3 id="constructors">Constructors</h3><a class="heading-link" href="#constructors" aria-label="Link to 'Constructors' section">#</a></div><p>This section discusses how constructors differ in Dart from JavaScript.</p><div class="header-wrapper"><h4 id="standard-constructor">Standard constructor</h4><a class="heading-link" href="#standard-constructor" aria-label="Link to 'Standard constructor' section">#</a></div><p>A standard class constructor looks very similar to a JavaScript constructor. In Dart, the <code>constructor</code> keyword is replaced by the full class name, and all parameters must be explicitly typed. In Dart, the <code>new</code> keyword was once required for creating class instances, but is now optional and its use is no longer recommended.</p><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"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> x;</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> y;</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#0468D7">double</span><span style="color:#222222"> x, </span><span style="color:#0468D7">double</span><span style="color:#222222"> y) : </span><span style="color:#D43324">this</span><span style="color:#222222">.x = x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y = y { }</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Create a new instance of the Point class</span></span> <span class="line"><span style="color:#0468D7">Point</span><span style="color:#222222"> p = </span><span style="color:#0468D7">Point</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="initializer-lists">Initializer lists</h4><a class="heading-link" href="#initializer-lists" aria-label="Link to 'Initializer lists' section">#</a></div><p>Use initializer lists to write your constructor. Insert the initializer list between the constructor's parameters and body.</p><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"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> ...</span></span> <span class="line"><span style="color:#0468D7"> Point</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">double</span><span style="color:#222222">> json)</span></span> <span class="line"><span style="color:#222222"> : x = json[</span><span style="color:#11796D">'x'</span><span style="color:#222222">]!,</span></span> <span class="line"><span style="color:#222222"> y = json[</span><span style="color:#11796D">'y'</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">'In Point.fromJson(): (</span><span style="color:#11796D">$</span><span style="color:#222222">x</span><span style="color:#11796D">, </span><span style="color:#11796D">$</span><span style="color:#222222">y</span><span style="color:#11796D">)'</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><div class="header-wrapper"><h4 id="constructor-parameters">Constructor parameters</h4><a class="heading-link" href="#constructor-parameters" aria-label="Link to 'Constructor parameters' section">#</a></div><p>Writing code to assign class fields in the constructor can feel like creating boilerplate code, so Dart has some syntactic sugar, called <a href="/language/constructors">initializing parameters</a> to make this easier:</p><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"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> double</span><span style="color:#222222"> x;</span></span> <span class="line"><span style="color:#0468D7"> double</span><span style="color:#222222"> y;</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // Syntactic sugar for setting x and y</span></span> <span class="line"><span style="color:#6E6E70"> // before the constructor body runs.</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Create a new instance of the Point class</span></span> <span class="line"><span style="color:#0468D7">Point</span><span style="color:#222222"> p = </span><span style="color:#0468D7">Point</span><span style="color:#222222">(</span><span style="color:#11796D">3</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">);</span></span></code></pre></div></div><p>Similar to functions, constructors have the option to take positioned or named parameters:</p><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"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> ...</span></span> <span class="line"><span style="color:#6E6E70"> // With an optional positioned parameter</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, [</span><span style="color:#D43324">this</span><span style="color:#222222">.y = </span><span style="color:#11796D">5</span><span style="color:#222222">]);</span></span> <span class="line"><span style="color:#6E6E70"> // With named parameters</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">({ </span><span style="color:#D43324">required</span><span style="color:#D43324"> this</span><span style="color:#222222">.y, </span><span style="color:#D43324">this</span><span style="color:#222222">.x = </span><span style="color:#11796D">5</span><span style="color:#222222"> });</span></span> <span class="line"><span style="color:#6E6E70"> // With both positional and named parameters</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> x, </span><span style="color:#0468D7">int</span><span style="color:#222222"> y, { boolean multiply }) {</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> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="named-constructors">Named constructors</h4><a class="heading-link" href="#named-constructors" aria-label="Link to 'Named constructors' section">#</a></div><p>Unlike JavaScript, Dart allows classes to have multiple constructors, by allowing you to name them. You can optionally have one single unnamed constructor, any additional constructors must be named:</p><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">const</span><span style="color:#0468D7"> double</span><span style="color:#222222"> xOrigin = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#0468D7"> double</span><span style="color:#222222"> yOrigin = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> double</span><span style="color:#222222"> x = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#0468D7"> double</span><span style="color:#222222"> y = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // Named constructor</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">.</span><span style="color:#6200EE">origin</span><span style="color:#222222">()</span></span> <span class="line"><span style="color:#222222"> : x = xOrigin,</span></span> <span class="line"><span style="color:#222222"> y = yOrigin;</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="const-constructors">Const constructors</h4><a class="heading-link" href="#const-constructors" aria-label="Link to 'Const constructors' section">#</a></div><p>To enable immutable class instances, use a <code>const</code> constructor. A class with a <code>const</code> constructor can have <code>final</code> instance variables only.</p><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"> ImmutablePoint</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> final</span><span style="color:#0468D7"> double</span><span style="color:#222222"> x, y;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> const</span><span style="color:#0468D7"> ImmutablePoint</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="constructor-redirection">Constructor redirection</h4><a class="heading-link" href="#constructor-redirection" aria-label="Link to 'Constructor redirection' section">#</a></div><p>You can call constructors from other constructors to prevent code duplication or to add additional defaults for parameters:</p><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"> Point</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> double</span><span style="color:#222222"> x, y;</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // The main constructor for this class.</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.x, </span><span style="color:#D43324">this</span><span style="color:#222222">.y);</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // Delegates to the main constructor.</span></span> <span class="line"><span style="color:#0468D7"> Point</span><span style="color:#222222">.</span><span style="color:#6200EE">alongXAxis</span><span style="color:#222222">(</span><span style="color:#0468D7">double</span><span style="color:#222222"> x) : </span><span style="color:#D43324">this</span><span style="color:#222222">(x, </span><span style="color:#11796D">0</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h4 id="factory-constructors">Factory constructors</h4><a class="heading-link" href="#factory-constructors" aria-label="Link to 'Factory constructors' section">#</a></div><p>You can use a factory constructor when you don't need to create a new class instance. One example would be when returning a cached instance:</p><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"> Logger</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> static</span><span style="color:#D43324"> final</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">Logger</span><span style="color:#222222">> _cache =</span></span> <span class="line"><span style="color:#222222"> <</span><span style="color:#0468D7">String</span><span style="color:#222222">, </span><span style="color:#0468D7">Logger</span><span style="color:#222222">>{};</span></span> <span class="line"><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:#222222"> </span></span> <span class="line"><span style="color:#6E6E70"> // Factory constructor that returns a cached copy,</span></span> <span class="line"><span style="color:#6E6E70"> // or creates a new one if it is not yet available.</span></span> <span class="line"><span style="color:#D43324"> factory</span><span style="color:#0468D7"> Logger</span><span style="color:#222222">(</span><span style="color:#0468D7">String</span><span style="color:#222222"> name) {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> _cache.</span><span style="color:#6200EE">putIfAbsent</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> name, () => _cache[name] ??= </span><span style="color:#0468D7">Logger</span><span style="color:#222222">.</span><span style="color:#6200EE">_internal</span><span style="color:#222222">(name);</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70"> // Private constructor for internal use only</span></span> <span class="line"><span style="color:#0468D7"> Logger</span><span style="color:#222222">.</span><span style="color:#6200EE">_internal</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.name);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="methods">Methods</h3><a class="heading-link" href="#methods" aria-label="Link to 'Methods' section">#</a></div><p>In both Dart and JavaScript, methods serve as functions that provide behavior for an object.</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">function</span><span style="color:#6200EE"> doSomething</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// This is a function</span></span> <span class="line"><span style="color:#6E6E70"> // Implementation..</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"> Example</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> doSomething</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// This is a method</span></span> <span class="line"><span style="color:#6E6E70"> // Implementation..</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="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"> doSomething</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// This is a function</span></span> <span class="line"><span style="color:#6E6E70"> // Implementation..</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"> Example</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> void</span><span style="color:#6200EE"> doSomething</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// This is a method</span></span> <span class="line"><span style="color:#6E6E70"> // Implementation..</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="extending-classes">Extending classes</h3><a class="heading-link" href="#extending-classes" aria-label="Link to 'Extending classes' section">#</a></div><p>Dart allows classes to extend another class, in the same way that JavaScript does.</p><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"> Animal</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> eyes;</span></span> <span class="line"><span style="color:#222222"> </span></span> <span class="line"><span style="color:#0468D7"> Animal</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.eyes);</span></span> <span class="line"><span style="color:#222222"> </span></span> <span class="line"><span style="color:#6200EE"> makeNoise</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">'???'</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> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Cat</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> Cat</span><span style="color:#222222">(): </span><span style="color:#D43324">super</span><span style="color:#222222">(</span><span style="color:#11796D">2</span><span style="color:#222222">);</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> @override</span></span> <span class="line"><span style="color:#6200EE"> makeNoise</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">'Meow'</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:#0468D7">Animal</span><span style="color:#222222"> animal = </span><span style="color:#0468D7">Cat</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#6200EE">print</span><span style="color:#222222">(animal.eyes); </span><span style="color:#6E6E70">// 2</span></span> <span class="line"><span style="color:#222222">animal.</span><span style="color:#6200EE">makeNoise</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Meow</span></span></code></pre></div></div><p>When overriding a method from the parent class, use the <code>@override</code> annotation. While this annotation is optional, it shows that the override is intentional. The Dart analyzer shows a warning if the method is not actually overriding a superclass method.</p><p>The parent method that is being overridden can still be called using the <code>super</code> keyword:</p><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"> Cat</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> ...</span></span> <span class="line"><span style="color:#D43324"> @override</span></span> <span class="line"><span style="color:#6200EE"> makeNoise</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">'Meow'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> super</span><span style="color:#222222">.</span><span style="color:#6200EE">makeNoise</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:#0468D7">Animal</span><span style="color:#222222"> animal = </span><span style="color:#0468D7">Cat</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">animal.</span><span style="color:#6200EE">makeNoise</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Meow</span></span> <span class="line"><span style="color:#6E6E70"> // ???</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="classes-as-interfaces">Classes as interfaces</h3><a class="heading-link" href="#classes-as-interfaces" aria-label="Link to 'Classes as interfaces' section">#</a></div><p>Like JavaScript, Dart doesn't have a separate definition for interfaces. However, unlike JavaScript, all class definitions double as an interface; you can implement a class as an interface using the <code>implements</code> keyword.</p><p>When a class is implemented as an interface, its public API must be implemented by the new class. Unlike <code>extends</code>, its method and field implementations aren't shared with the new class. While a class can only extend a single class, you can implement multiple interfaces at a time, even when the implementing class already extends another.</p><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"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> consume</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">'Eating food...'</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:#D43324">class</span><span style="color:#0468D7"> Cat</span><span style="color:#D43324"> implements</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> consume</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">'Eating mice...'</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:#0468D7">Consumer</span><span style="color:#222222"> consumer = </span><span style="color:#0468D7">Cat</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">consumer.</span><span style="color:#6200EE">consume</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Eating mice</span></span></code></pre></div></div><p>When implementing an interface, the <code>super</code> method can't be called as the method bodies are not inherited:</p><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"> Cat</span><span style="color:#D43324"> implements</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#D43324"> @override</span></span> <span class="line"><span style="color:#6200EE"> consume</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">'Eating mice...'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#D43324"> super</span><span style="color:#222222">.</span><span style="color:#6200EE">consume</span><span style="color:#222222">(); </span></span> <span class="line"><span style="color:#6E6E70"> // Invalid. The superclass `Object` has no `consume` method.</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="abstract-classes-and-methods">Abstract classes and methods</h3><a class="heading-link" href="#abstract-classes-and-methods" aria-label="Link to 'Abstract classes and methods' section">#</a></div><p>To ensure that a class can only be extended or have its interface implemented, but to disallow the construction of any instances, mark it as <code>abstract</code>.</p><p>Classes marked as <code>abstract</code> can have abstract methods, which do not require a body and are instead required to be implemented when the class is either extended or its interface is implemented:</p><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">abstract</span><span style="color:#D43324"> class</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> consume</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#6E6E70">// Extending the full class</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Dog</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> consume</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">'Eating cookies...'</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:#6E6E70">// Just implementing the interface</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Cat</span><span style="color:#D43324"> implements</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> consume</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">'Eating mice...'</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:#0468D7">Consumer</span><span style="color:#222222"> consumer;</span></span> <span class="line"><span style="color:#222222">consumer = </span><span style="color:#0468D7">Dog</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">consumer.</span><span style="color:#6200EE">consume</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Eating cookies...</span></span> <span class="line"><span style="color:#222222">consumer = </span><span style="color:#0468D7">Cat</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">consumer.</span><span style="color:#6200EE">consume</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Eating mice...</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="mixins">Mixins</h3><a class="heading-link" href="#mixins" aria-label="Link to 'Mixins' section">#</a></div><p>Mixins are used to share functionality between classes. You can use the mixin's fields and methods in the class, using their functionality as if it were part of the class. A class can use multiple mixins. This helps when multiple classes share the same functionality, without needing to inherit from each other or share a common ancestor.</p><p>Use the <code>with</code> keyword to add one or more comma-separated mixins to a class.</p><p>JavaScript has no keyword equivalent. JavaScript can use <code>Object.assign</code> to merge additional objects into an existing object, after instantiating.</p><p>The following examples show how JavaScript and Dart achieve similar behavior:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Animal</span><span style="color:#222222"> {}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Defining the mixins</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Flyer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> fly</span><span style="color:#222222"> = () </span><span style="color:#D43324">=></span><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(</span><span style="color:#11796D">'Flaps wings'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Walker</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> walk</span><span style="color:#222222"> = () </span><span style="color:#D43324">=></span><span style="color:#222222"> console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(</span><span style="color:#11796D">'Walks on legs'</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:#D43324">class</span><span style="color:#0468D7"> Bat</span><span style="color:#D43324"> extends</span><span style="color:#222222"> Animal {}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Goose</span><span style="color:#D43324"> extends</span><span style="color:#222222"> Animal {}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Dog</span><span style="color:#D43324"> extends</span><span style="color:#222222"> Animal {}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Composing the class instances with</span></span> <span class="line"><span style="color:#6E6E70">// their correct functionality.</span></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> bat =</span></span> <span class="line"><span style="color:#222222"> Object.</span><span style="color:#6200EE">assign</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Bat</span><span style="color:#222222">(),</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Flyer</span><span style="color:#222222">()</span></span> <span class="line"><span style="color:#222222"> );</span></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> goose =</span></span> <span class="line"><span style="color:#222222"> Object.</span><span style="color:#6200EE">assign</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Goose</span><span style="color:#222222">(),</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Flyer</span><span style="color:#222222">(),</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Walker</span><span style="color:#222222">()</span></span> <span class="line"><span style="color:#222222"> );</span></span> <span class="line"><span style="color:#D43324">const</span><span style="color:#222222"> dog =</span></span> <span class="line"><span style="color:#222222"> Object.</span><span style="color:#6200EE">assign</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Dog</span><span style="color:#222222">(),</span></span> <span class="line"><span style="color:#222222"> new </span><span style="color:#6200EE">Walker</span><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:#6E6E70">// Correct calls</span></span> <span class="line"><span style="color:#222222">bat.</span><span style="color:#6200EE">fly</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">goose.</span><span style="color:#6200EE">fly</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">goose.</span><span style="color:#6200EE">walk</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">dog.</span><span style="color:#6200EE">walk</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#6E6E70">// Incorrect calls</span></span> <span class="line"><span style="color:#222222">bat.</span><span style="color:#6200EE">walk</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// `bat` lacks the `walk` method</span></span> <span class="line"><span style="color:#222222">dog.</span><span style="color:#6200EE">fly</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// `dog` lacks the `fly` method</span></span></code></pre></div></div><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">abstract</span><span style="color:#D43324"> class</span><span style="color:#0468D7"> Animal</span><span style="color:#222222"> {}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Defining the mixins</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Flyer</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> fly</span><span style="color:#222222">() => </span><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#11796D">'Flaps wings'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Walker</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> walk</span><span style="color:#222222">() => </span><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#11796D">'Walks on legs'</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:#D43324">class</span><span style="color:#0468D7"> Bat</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#D43324"> with</span><span style="color:#0468D7"> Flyer</span><span style="color:#222222"> {}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Goose</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#D43324"> with</span><span style="color:#0468D7"> Flyer</span><span style="color:#222222">, </span><span style="color:#0468D7">Walker</span><span style="color:#222222"> {}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Dog</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#D43324"> with</span><span style="color:#0468D7"> Walker</span><span style="color:#222222"> {}</span></span> <span class="line"></span> <span class="line"><span style="color:#6E6E70">// Correct calls</span></span> <span class="line"><span style="color:#0468D7">Bat</span><span style="color:#222222">().</span><span style="color:#6200EE">fly</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#0468D7">Goose</span><span style="color:#222222">().</span><span style="color:#6200EE">fly</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#0468D7">Goose</span><span style="color:#222222">().</span><span style="color:#6200EE">walk</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#0468D7">Dog</span><span style="color:#222222">().</span><span style="color:#6200EE">walk</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#6E6E70">// Incorrect calls</span></span> <span class="line"><span style="color:#0468D7">Bat</span><span style="color:#222222">().</span><span style="color:#6200EE">walk</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Not using the Walker mixin</span></span> <span class="line"><span style="color:#0468D7">Dog</span><span style="color:#222222">().</span><span style="color:#6200EE">fly</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Not using the Flyer mixin</span></span></code></pre></div></div><p>Alternatively, you can replace the <code>class</code> keyword with <code>mixin</code> to prevent the mixin from being used as a regular class:</p><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">mixin</span><span style="color:#0468D7"> Walker</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#6200EE"> walk</span><span style="color:#222222">() => </span><span style="color:#6200EE">print</span><span style="color:#222222">(</span><span style="color:#11796D">'Walks legs'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#6E6E70">// Not possible, as Walker is no longer a class.</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Bat</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Walker</span><span style="color:#222222"> {}</span></span></code></pre></div></div><p>Since you can use multiple mixins, they can have overlapping methods or fields with each other when used on the same class. They can even overlap with the class that uses them, or that class's superclass. The order in which they are added to a class matters.</p><p>To give an example:</p><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"> Bird</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> Animal</span><span style="color:#D43324"> with</span><span style="color:#0468D7"> Consumer</span><span style="color:#222222">, </span><span style="color:#0468D7">Flyer</span><span style="color:#222222"> {</span></span></code></pre></div></div><p>When a method is called on an instance of <code>Bird</code>, Dart starts with its own class, <code>Bird</code>, which takes precedence over other implementations. If <code>Bird</code> has no implementation, then <code>Flyer</code> is checked, followed by <code>Consumer</code>, until an implementation is found. The parent class, <code>Animal</code>, is checked last.</p><div class="header-wrapper"><h3 id="extensions">Extensions</h3><a class="heading-link" href="#extensions" aria-label="Link to 'Extensions' section">#</a></div><p>Extending classes, implementing interfaces, or using mixins all work when the affected class is editable. However, sometimes it's useful to extend a class that already exists or is part of another library or the Dart SDK.</p><p>In these cases, Dart offers the ability to write extensions for existing classes.</p><p>As an example, the following extension on the <code>String</code> class from the Dart SDK allows parsing of integers:</p><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">extension</span><span style="color:#0468D7"> NumberParsing</span><span style="color:#D43324"> on</span><span style="color:#0468D7"> String</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#6200EE"> parseInt</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#0468D7"> int</span><span style="color:#222222">.</span><span style="color:#6200EE">parse</span><span style="color:#222222">(</span><span style="color:#D43324">this</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>For the extension to become available, it has to be present in the same file, or its file must be imported.</p><p>Use it as follows:</p><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"> 'string_apis.dart'</span><span style="color:#222222">; </span><span style="color:#6E6E70">// Import the file the extension is in</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> age = </span><span style="color:#11796D">'42'</span><span style="color:#222222">.</span><span style="color:#6200EE">parseInt</span><span style="color:#222222">(); </span><span style="color:#6E6E70">// Use the extension method.</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="getters-and-setters">Getters and setters</h3><a class="heading-link" href="#getters-and-setters" aria-label="Link to 'Getters and setters' section">#</a></div><p>Getters and setters in Dart work exactly like their JavaScript counterparts:</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Person</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> _age = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> get</span><span style="color:#6200EE"> age</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#D43324"> this</span><span style="color:#222222">._age;</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> set</span><span style="color:#6200EE"> age</span><span style="color:#222222">(value) {</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (value < </span><span style="color:#11796D">0</span><span style="color:#222222">) {</span></span> <span class="line"><span style="color:#D43324"> throw</span><span style="color:#222222"> new </span><span style="color:#6200EE">Error</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#11796D"> 'age cannot be negative'</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:#D43324"> this</span><span style="color:#222222">._age = value;</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">var</span><span style="color:#222222"> person = new </span><span style="color:#6200EE">Person</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222">person.age = </span><span style="color:#11796D">10</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222">console.</span><span style="color:#6200EE">log</span><span style="color:#222222">(person.age);</span></span></code></pre></div></div><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"> Person</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> _age = </span><span style="color:#11796D">0</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#222222"> </span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#D43324"> get</span><span style="color:#222222"> age {</span></span> <span class="line"><span style="color:#D43324"> return</span><span style="color:#222222"> _age;</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:#D43324"> set</span><span style="color:#6200EE"> age</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> value) {</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (value < </span><span style="color:#11796D">0</span><span style="color:#222222">) {</span></span> <span class="line"><span style="color:#D43324"> throw</span><span style="color:#0468D7"> ArgumentError</span><span style="color:#222222">(</span></span> <span class="line"><span style="color:#11796D"> 'Age cannot be negative'</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"> _age = value;</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">void</span><span style="color:#6200EE"> main</span><span style="color:#222222">() {</span></span> <span class="line"><span style="color:#D43324"> var</span><span style="color:#222222"> person = </span><span style="color:#0468D7">Person</span><span style="color:#222222">();</span></span> <span class="line"><span style="color:#222222"> person.age = </span><span style="color:#11796D">10</span><span style="color:#222222">;</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(person.age);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="public-and-private-members">Public and private members</h3><a class="heading-link" href="#public-and-private-members" aria-label="Link to 'Public and private members' section">#</a></div><p>Like JavaScript, Dart has no access modifier keywords: all class members are public by default.</p><p>JavaScript will include private class members in the next practical revision of the EcmaScript standard. As such, implementations for this have been available in various browsers and runtimes for some time.</p><p>To make a class member private in JavaScript, prefix its name with a pound (or hash) symbol (<code>#</code>).</p><div class="code-block-wrapper language-js"><div class="code-block-body"><span class="code-block-language" title="Language js">js</span><pre class="shiki dash-light" tabindex="0"><code><span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Animal</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#222222"> eyes; </span><span style="color:#6E6E70">// Public field</span></span> <span class="line"><span style="color:#222222"> #paws; </span><span style="color:#6E6E70">// Private field</span></span> <span class="line"></span> <span class="line"><span style="color:#222222"> #</span><span style="color:#6200EE">printEyes</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// Private method</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.eyes);</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#6200EE"> printPaws</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// Public method</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.#paws);</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 make a class member private in Dart, prefix its name with an underscore (<code>_</code>).</p><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"> Animal</span><span style="color:#222222"> {</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> eyes; </span><span style="color:#6E6E70">// Public field</span></span> <span class="line"><span style="color:#0468D7"> int</span><span style="color:#222222"> _paws; </span><span style="color:#6E6E70">// Private field</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> void</span><span style="color:#6200EE"> _printEyes</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// Private method</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.eyes);</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"></span> <span class="line"><span style="color:#D43324"> void</span><span style="color:#6200EE"> printPaws</span><span style="color:#222222">() { </span><span style="color:#6E6E70">// Public method</span></span> <span class="line"><span style="color:#6200EE"> print</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">._paws);</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>JavaScript uses the hash as a convention. Dart's compiler enforces use of the underscore for this feature.</p><p>Dart makes private members private to the library, not the class. This means that you can access private members from code in the same library. By default, Dart limits access to private class members to code in the same file. To expand the scope of a library beyond one file, add the <code>part</code> directive. When possible, <a href="/guides/libraries/create-packages#organizing-a-package">avoid using <code>part</code></a>. Reserve using <code>part</code> for code generators.</p><div class="header-wrapper"><h3 id="late-variables">Late variables</h3><a class="heading-link" href="#late-variables" aria-label="Link to 'Late variables' section">#</a></div><p>To indicate that Dart initializes class fields at a later point, assign the <code>late</code> keyword to those class fields. Those class fields remain non-nullable. Do this when a variable doesn't need to observed or accessed immediately and can be initialized later. This differs from labeling the field as nullable.</p><ul><li><p>(Non-nullable) late fields cannot have null assigned at a later point.</p></li><li><p>(Non-nullable) late fields throw a runtime error when accessed before they initialize. This should be avoided.</p></li></ul><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"> PetOwner</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"> late</span><span style="color:#D43324"> final</span><span style="color:#0468D7"> Pet</span><span style="color:#222222"> _pet;</span></span> <span class="line"><span style="color:#0468D7"> PetOwner</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.name, </span><span style="color:#0468D7">String</span><span style="color:#222222"> petName) {</span></span> <span class="line"><span style="color:#6E6E70"> // Cyclic object graph, cannot set _pet before owner exists.</span></span> <span class="line"><span style="color:#222222"> _pet = </span><span style="color:#0468D7">Pet</span><span style="color:#222222">(petName, </span><span style="color:#D43324">this</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#222222"> }</span></span> <span class="line"><span style="color:#0468D7"> Pet</span><span style="color:#D43324"> get</span><span style="color:#222222"> pet => _pet;</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#D43324">class</span><span style="color:#0468D7"> Pet</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"> PetOwner</span><span style="color:#222222"> owner;</span></span> <span class="line"><span style="color:#0468D7"> Pet</span><span style="color:#222222">(</span><span style="color:#D43324">this</span><span style="color:#222222">.name, </span><span style="color:#D43324">this</span><span style="color:#222222">.owner);</span></span> <span class="line"><span style="color:#222222">}</span></span></code></pre></div></div><p>Use <code>late</code> for local variables only if unclear code results in the compiler being unable determine if the code initialized the variable.</p><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:#6200EE">doSomething</span><span style="color:#222222">(</span><span style="color:#0468D7">int</span><span style="color:#222222"> n, </span><span style="color:#0468D7">bool</span><span style="color:#222222"> capture) {</span></span> <span class="line"><span style="color:#D43324"> late</span><span style="color:#0468D7"> List</span><span style="color:#222222"><</span><span style="color:#0468D7">Foo</span><span style="color:#222222">> captures;</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (capture) captures = [];</span></span> <span class="line"><span style="color:#D43324"> for</span><span style="color:#222222"> (</span><span style="color:#D43324">var</span><span style="color:#222222"> i = </span><span style="color:#11796D">0</span><span style="color:#222222">; i < n; i++) {</span></span> <span class="line"><span style="color:#D43324"> var</span><span style="color:#222222"> foo = </span><span style="color:#6200EE">something</span><span style="color:#222222">(i);</span></span> <span class="line"><span style="color:#D43324"> if</span><span style="color:#222222"> (capture) captures.</span><span style="color:#6200EE">add</span><span style="color:#222222">(foo);</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>In the preceding example, the compiler does not know to assign <code>captures</code> if <code>capture</code> is true. Using <code>late</code> delays the normal "assigned" checks until runtime.</p><div class="header-wrapper"><h2 id="generics">Generics</h2><a class="heading-link" href="#generics" aria-label="Link to 'Generics' section">#</a></div><p>While JavaScript doesn't offer generics, Dart does to improve type safety and reduce code duplication.</p><div class="header-wrapper"><h3 id="generic-methods">Generic methods</h3><a class="heading-link" href="#generic-methods" aria-label="Link to 'Generic methods' section">#</a></div><p>You can apply generics to methods. To define a generic type parameter, place it between angle brackets <code>< ></code> after the method name. You can then use this type within the method as the return type or within the method's parameters:</p><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">Map</span><span style="color:#222222"><</span><span style="color:#0468D7">Object</span><span style="color:#222222">?, </span><span style="color:#0468D7">Object</span><span style="color:#222222">?> _cache = {};</span></span> <span class="line"><span style="color:#0468D7">T</span><span style="color:#6200EE"> cache</span><span style="color:#222222"><</span><span style="color:#0468D7">T</span><span style="color:#222222">>(</span><span style="color:#0468D7">T</span><span style="color:#222222"> value) => (_cache[value] ??= value) </span><span style="color:#D43324">as</span><span style="color:#0468D7"> T</span><span style="color:#222222">;</span></span></code></pre></div></div><p>Define multiple generic types by separating them with a comma:</p><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">// Defining a method with multiple generics.</span></span> <span class="line"><span style="color:#0468D7">T</span><span style="color:#6200EE"> transform</span><span style="color:#222222"><</span><span style="color:#0468D7">T</span><span style="color:#222222">, </span><span style="color:#0468D7">Q</span><span style="color:#222222">>(</span><span style="color:#0468D7">T</span><span style="color:#222222"> param1, </span><span style="color:#0468D7">Q</span><span style="color:#222222"> param2) {</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:#6E6E70">// Calling the method with explicitly defined types.</span></span> <span class="line"><span style="color:#6200EE">transform</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">, </span><span style="color:#0468D7">String</span><span style="color:#222222">>(</span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">'string value'</span><span style="color:#222222">);</span></span> <span class="line"><span style="color:#6E6E70">// Types are optional when the analyzer can infer them.</span></span> <span class="line"><span style="color:#6200EE">transform</span><span style="color:#222222">(</span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">'string value'</span><span style="color:#222222">);</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="generic-classes">Generic classes</h3><a class="heading-link" href="#generic-classes" aria-label="Link to 'Generic classes' section">#</a></div><p>Generics can also be applied to classes. You can include the type to use when calling a constructor. This allows you to tailor reusable classes to specific types.</p><p>In the following example, the <code>Cache</code> class caches specific types:</p><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"> Cache</span><span style="color:#222222"><</span><span style="color:#0468D7">T</span><span style="color:#222222">> {</span></span> <span class="line"><span style="color:#0468D7"> T</span><span style="color:#6200EE"> getByKey</span><span style="color:#222222">(</span><span style="color:#0468D7">String</span><span style="color:#222222"> key) {}</span></span> <span class="line"><span style="color:#D43324"> void</span><span style="color:#6200EE"> setByKey</span><span style="color:#222222">(</span><span style="color:#0468D7">String</span><span style="color:#222222"> key, </span><span style="color:#0468D7">T</span><span style="color:#222222"> value) {}</span></span> <span class="line"><span style="color:#222222">}</span></span> <span class="line"><span style="color:#6E6E70">// Creating a cache for strings</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> stringCache = </span><span style="color:#0468D7">Cache</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">>(); </span><span style="color:#6E6E70">// stringCache has type Cache<String></span></span> <span class="line"><span style="color:#222222">stringCache.</span><span style="color:#6200EE">setByKey</span><span style="color:#222222">(</span><span style="color:#11796D">'Foo'</span><span style="color:#222222">, </span><span style="color:#11796D">'Bar'</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Valid, setting a string value.</span></span> <span class="line"><span style="color:#222222">stringCache.</span><span style="color:#6200EE">setByKey</span><span style="color:#222222">(</span><span style="color:#11796D">'Baz'</span><span style="color:#222222">, </span><span style="color:#11796D">5</span><span style="color:#222222">); </span><span style="color:#6E6E70">// Invalid, int type does not match generic.</span></span></code></pre></div></div><p>If you omit the type declaration, the runtime type becomes <code>Cache<dynamic></code> and both calls to <code>setByKey</code> are valid.</p><div class="header-wrapper"><h3 id="restricting-generics">Restricting generics</h3><a class="heading-link" href="#restricting-generics" aria-label="Link to 'Restricting generics' section">#</a></div><p>You can use generics to restrict your code to a family of types using <code>extends</code>. This ensures that your class is instantiated with a generic type that extends a specific type:</p><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"> NumberManager</span><span style="color:#222222"><</span><span style="color:#0468D7">T</span><span style="color:#D43324"> extends</span><span style="color:#0468D7"> num</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:#6E6E70">// Valid.</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> manager = </span><span style="color:#0468D7">NumberManager</span><span style="color:#222222"><</span><span style="color:#0468D7">int</span><span style="color:#222222">>();</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> manager = </span><span style="color:#0468D7">NumberManager</span><span style="color:#222222"><</span><span style="color:#0468D7">double</span><span style="color:#222222">>();</span></span> <span class="line"><span style="color:#6E6E70">// Invalid, String nor its parent classes extend num.</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> manager = </span><span style="color:#0468D7">NumberManager</span><span style="color:#222222"><</span><span style="color:#0468D7">String</span><span style="color:#222222">>();</span></span></code></pre></div></div><div class="header-wrapper"><h3 id="generics-in-literals">Generics in literals</h3><a class="heading-link" href="#generics-in-literals" aria-label="Link to 'Generics in literals' section">#</a></div><p><code>Map</code>, <code>Set</code>, and <code>List</code> literals can accept type arguments. This helps when Dart cannot infer the type or infer the type correctly.</p><p>For example, the <code>List</code> class has a generic definition: <code>class List<E></code>. The type parameter <code>E</code> refers to the type of the list's contents. Normally, this type is automatically inferred, which is used in some <code>List</code> class's member types. (For example, its first getter returns a value of type <code>E</code>.) When defining a <code>List</code> literal, you can explicitly define the generic type as follows:</p><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">// Automatic type inference</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> objList = [</span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">2.0</span><span style="color:#222222">]; </span><span style="color:#6E6E70">// Type: List<num></span></span> <span class="line"><span style="color:#6E6E70">// Explicit type definition:</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> objList = <</span><span style="color:#0468D7">Object</span><span style="color:#222222">>[</span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">2.0</span><span style="color:#222222">]; </span><span style="color:#6E6E70">// Type: List<Object></span></span> <span class="line"><span style="color:#6E6E70">// Sets work identically:</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> objSet = <</span><span style="color:#0468D7">Object</span><span style="color:#222222">>{</span><span style="color:#11796D">5</span><span style="color:#222222">, </span><span style="color:#11796D">2.0</span><span style="color:#222222">};</span></span></code></pre></div></div><p>This is also true for <code>Map</code>s, which also define their key and value types using generics (<code>class Map<K, V></code>):</p><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">// Automatic type inference</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> map = {</span></span> <span class="line"><span style="color:#11796D"> 'foo'</span><span style="color:#222222">: </span><span style="color:#11796D">'bar'</span></span> <span class="line"><span style="color:#222222">}; </span><span style="color:#6E6E70">// Type: Map<String, String></span></span> <span class="line"><span style="color:#6E6E70">// Explicit type definition:</span></span> <span class="line"><span style="color:#D43324">var</span><span style="color:#222222"> map = <</span><span style="color:#0468D7">String</span><span style="color:#222222">, </span><span style="color:#0468D7">Object</span><span style="color:#222222">>{</span></span> <span class="line"><span style="color:#11796D"> 'foo'</span><span style="color:#222222">: </span><span style="color:#11796D">'bar'</span></span> <span class="line"><span style="color:#222222">}; </span><span style="color:#6E6E70">// Type: Map<String, Object></span></span></code></pre></div></div><div class="header-wrapper"><h2 id="doc-comments">Doc comments</h2><a class="heading-link" href="#doc-comments" aria-label="Link to 'Doc comments' section">#</a></div><p>Regular comments work the same in Dart as they do in JavaScript. Using <code>//</code> comments out everything beyond it for the remaining line, and you can use <code>/* ... */</code> to block comments spanning multiple lines.</p><p>In addition to regular comments, Dart also has <a href="/effective-dart/documentation#doc-comments">doc comments</a> that work in tandem with <a href="/tools/dart-doc"><code>dart doc</code></a>: a first party tool that generates HTML documentation for Dart packages. It's considered best practice to place doc comments above all declarations for public members.</p><p>Define a doc comment by using three forward slashes instead of two (<code>///</code>):</p><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">/// The number of characters in this chunk when unsplit.</span></span> <span class="line"><span style="color:#0468D7">int</span><span style="color:#D43324"> get</span><span style="color:#222222"> length => ...</span></span></code></pre></div></div><div class="header-wrapper"><h2 id="next-steps">Next steps</h2><a class="heading-link" href="#next-steps" aria-label="Link to 'Next steps' section">#</a></div><p>This guide has introduced you to the major differences between Dart and JavaScript. At this point, consider reading the Dart documentation. You could also read the <a href="https://flutter.dev">Flutter</a> docs. Built with Dart, Flutter is an open-source framework that uses Dart for building natively compiled, multi-platform applications from a single codebase. These docs provide in-depth information about the language and practical ways of getting started.</p><p>Some possible next steps:</p><ul><li><a href="/language">Language tour</a> to learn more about the Dart language</li><li><a href="/libraries">Core library documentation</a> to learn about Dart's core libraries</li><li><a href="/tutorials">Dart tutorials</a> for applied practice covering a variety of topics</li><li><a href="/effective-dart">Effective Dart</a> to learn about common conventions and guidelines when writing Dart code</li></ul><p id="page-github-links"><span>Unless stated otherwise, the documentation on this site reflects Dart 3.5.4. Page last updated on 2024-10-22.</span> <a href="https://github.com/dart-lang/site-www/tree/main/src/content/resources/coming-from/js-to-dart.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/resources/coming-from/js-to-dart/&page-source=https://github.com/dart-lang/site-www/tree/main/src/content/resources/coming-from/js-to-dart.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>