Google uses cookies to deliver its services, to personalize ads, and to
analyze traffic. You can adjust your privacy controls anytime in your
Google settings <https://myaccount.google.com/data-and-personalization>.
Learn more <https://policies.google.com/technologies/cookies>.
Okay
/menu/
Dart <https://dart.dev/>
* Overview <https://dart.dev/overview>
* Docs <https://dart.dev/guides>
* Community <https://dart.dev/community>
* Try Dart <https://dart.dev/#try-dart>
* Get Dart <https://dart.dev/get-dart>
*
Dart 3.1 is out, with updates supporting the new features from Dart 3.0.
Check out the blog post <https://medium.com/dartlang/a1f4b3a7cdda>!
* Overview <https://dart.dev/overview>
* Community <https://dart.dev/community>
* Try Dart <https://dartpad.dev/>
* Get Dart <https://dart.dev/get-dart>
* Docs <https://dart.dev/guides>
* Tutorials & codelabs <#sidenav-1>
o Tutorials <https://dart.dev/tutorials>
o Codelabs <#sidenav-1-2>
+ List of Dart codelabs <https://dart.dev/codelabs>
+ Language cheatsheet <https://dart.dev/codelabs/dart-cheatsheet>
+ Iterable collections <https://dart.dev/codelabs/iterables>
+ Asynchronous programming
<https://dart.dev/codelabs/async-await>
+ Null safety <https://dart.dev/codelabs/null-safety>
* Language <#sidenav-2>
o Introduction <https://dart.dev/language>
o Syntax basics <#sidenav-2-2>
+ Variables <https://dart.dev/language/variables>
+ Operators <https://dart.dev/language/operators>
+ Comments <https://dart.dev/language/comments>
+ Metadata <https://dart.dev/language/metadata>
+ Libraries & imports <https://dart.dev/language/libraries>
+ Keywords <https://dart.dev/language/keywords>
o Types <#sidenav-2-3>
+ Built-in types <https://dart.dev/language/built-in-types>
+ Records <https://dart.dev/language/records>
+ Collections <https://dart.dev/language/collections>
+ Generics <https://dart.dev/language/generics>
+ Typedefs <https://dart.dev/language/typedefs>
+ Type system <https://dart.dev/language/type-system>
o Patterns <#sidenav-2-4>
+ Overview & usage <https://dart.dev/language/patterns>
+ Pattern types <https://dart.dev/language/pattern-types>
o Functions <https://dart.dev/language/functions>
o Control flow <#sidenav-2-6>
+ Loops <https://dart.dev/language/loops>
+ Branches <https://dart.dev/language/branches>
o Error handling <https://dart.dev/language/error-handling>
o Classes & objects <#sidenav-2-8>
+ Classes <https://dart.dev/language/classes>
+ Constructors <https://dart.dev/language/constructors>
+ Methods <https://dart.dev/language/methods>
+ Extend a class <https://dart.dev/language/extend>
+ Mixins <https://dart.dev/language/mixins>
+ Enums <https://dart.dev/language/enums>
+ Extension methods <https://dart.dev/language/extension-methods>
+ Callable objects <https://dart.dev/language/callable-objects>
o Class modifiers <#sidenav-2-9>
+ Overview & usage <https://dart.dev/language/class-modifiers>
+ Class modifiers for API maintainers
<https://dart.dev/language/class-modifiers-for-apis>
+ Reference <https://dart.dev/language/modifier-reference>
o Concurrency <#sidenav-2-10>
+ Asynchronous support <https://dart.dev/language/async>
+ Isolates <https://dart.dev/language/concurrency>
o Null safety <#sidenav-2-11>
+ Sound null safety <https://dart.dev/null-safety>
+ Migrating to null safety
<https://dart.dev/null-safety/migration-guide>
+ Understanding null safety
<https://dart.dev/null-safety/understanding-null-safety>
+ Unsound null safety
<https://dart.dev/null-safety/unsound-null-safety>
+ FAQ <https://dart.dev/null-safety/faq>
* Effective Dart <#sidenav-3>
o Overview <https://dart.dev/effective-dart>
o Style <https://dart.dev/effective-dart/style>
o Documentation <https://dart.dev/effective-dart/documentation>
o Usage <https://dart.dev/effective-dart/usage>
o Design <https://dart.dev/effective-dart/design>
* Core libraries <#sidenav-4>
o Overview <https://dart.dev/guides/libraries>
o Tour <https://dart.dev/guides/libraries/library-tour>
o Articles <#sidenav-4-3>
+ Creating streams
<https://dart.dev/articles/libraries/creating-streams>
* Packages <#sidenav-5>
o How to use packages <https://dart.dev/guides/packages>
o Commonly used packages
<https://dart.dev/guides/libraries/useful-libraries>
o Creating packages
<https://dart.dev/guides/libraries/create-packages>
o Publishing packages <https://dart.dev/tools/pub/publishing>
o Writing package pages
<https://dart.dev/guides/libraries/writing-package-pages>
o Package reference <#sidenav-5-6>
+ Dependencies <https://dart.dev/tools/pub/dependencies>
+ Glossary <https://dart.dev/tools/pub/glossary>
+ Package layout conventions
<https://dart.dev/tools/pub/package-layout>
+ Pub environment variables
<https://dart.dev/tools/pub/environment-variables>
+ Pubspec file <https://dart.dev/tools/pub/pubspec>
+ Troubleshooting pub <https://dart.dev/tools/pub/troubleshoot>
+ Verified publishers
<https://dart.dev/tools/pub/verified-publishers>
+ Versioning <https://dart.dev/tools/pub/versioning>
* Development <#sidenav-6>
o Futures, async, await <https://dart.dev/codelabs/async-await>
o Streams <https://dart.dev/tutorials/language/streams>
o JSON <https://dart.dev/guides/json>
o Number representation <https://dart.dev/guides/language/numbers>
o Interoperability <#sidenav-6-5>
+ C interop <https://dart.dev/guides/libraries/c-interop>
+ Objective-C & Swift interop
<https://dart.dev/guides/libraries/objective-c-interop>
+ Java & Kotlin interop
<https://dart.dev/guides/libraries/java-interop>
+ JavaScript interop <https://dart.dev/web/js-interop>
o Google APIs <https://dart.dev/guides/google-apis>
o Multi-platform apps <https://dart.dev/multiplatform-apps>
o Command-line & server apps <#sidenav-6-8>
+ Overview <https://dart.dev/server>
+ Get started <https://dart.dev/tutorials/server/get-started>
+ Write command-line apps
<https://dart.dev/tutorials/server/cmdline>
+ Fetch data from the internet
<https://dart.dev/tutorials/server/fetch-data>
+ Write HTTP servers
<https://dart.dev/tutorials/server/httpserver>
+ Libraries & packages <https://dart.dev/server/libraries>
+ Google Cloud <https://dart.dev/server/google-cloud>
o Web apps <#sidenav-6-9>
+ Overview <https://dart.dev/web>
+ Get started <https://dart.dev/tutorials/web/get-started>
+ Low-level web programming <#sidenav-6-9-3>
# Connect Dart & HTML
<https://dart.dev/tutorials/web/low-level-html/connect-dart-html>
# Add elements to the DOM
<https://dart.dev/tutorials/web/low-level-html/add-elements>
# Remove DOM elements
<https://dart.dev/tutorials/web/low-level-html/remove-elements>
+ Deployment <https://dart.dev/web/deployment>
+ Libraries & packages <https://dart.dev/web/libraries>
o Environment declarations
<https://dart.dev/guides/environment-declarations>
* Tools & techniques <#sidenav-7>
o Overview <https://dart.dev/tools>
o Editors & debuggers <#sidenav-7-2>
+ IntelliJ & Android Studio
<https://dart.dev/tools/jetbrains-plugin>
+ VS Code <https://dart.dev/tools/vs-code>
+ Dart DevTools <https://dart.dev/tools/dart-devtools>
+ DartPad <#sidenav-7-2-4>
# Overview <https://dart.dev/tools/dartpad>
# DartPad in tutorials
<https://dart.dev/tools/dartpad/dartpad-best-practices>
# Troubleshooting DartPad
<https://dart.dev/tools/dartpad/troubleshoot>
o Command-line tools <#sidenav-7-3>
+ Dart SDK <#sidenav-7-3-1>
# Overview <https://dart.dev/tools/sdk>
# dart <https://dart.dev/tools/dart-tool>
# dart analyze <https://dart.dev/tools/dart-analyze>
# dart compile <https://dart.dev/tools/dart-compile>
# dart create <https://dart.dev/tools/dart-create>
# dart doc <https://dart.dev/tools/dart-doc>
# dart fix <https://dart.dev/tools/dart-fix>
# dart format <https://dart.dev/tools/dart-format>
# dart info <https://dart.dev/tools/dart-info>
# dart pub <https://dart.dev/tools/pub/cmd>
# dart run <https://dart.dev/tools/dart-run>
# dart test <https://dart.dev/tools/dart-test>
# dartaotruntime <https://dart.dev/tools/dartaotruntime>
# Experiment flags <https://dart.dev/tools/experiment-flags>
+ Other command-line tools <#sidenav-7-3-2>
# build_runner <https://dart.dev/tools/build_runner>
# webdev <https://dart.dev/tools/webdev>
o Managing source code <#sidenav-7-4>
+ Formatting code <https://dart.dev/guides/language/formatting>
+ What not to commit
<https://dart.dev/guides/libraries/private-files>
o Static analysis <#sidenav-7-5>
+ Customizing static analysis <https://dart.dev/tools/analysis>
+ Fixing common type problems
<https://dart.dev/guides/language/sound-problems>
+ Diagnostic messages
<https://dart.dev/tools/diagnostic-messages>
+ Linter rules <https://dart.dev/tools/linter-rules>
o Testing & optimization <#sidenav-7-6>
+ Testing <https://dart.dev/guides/testing>
+ Debugging web apps <https://dart.dev/web/debugging>
* Resources <#sidenav-8>
o FAQ <https://dart.dev/resources/faq>
o Breaking changes <https://dart.dev/resources/breaking-changes>
o Language evolution <https://dart.dev/guides/language/evolution>
o Language specification <https://dart.dev/guides/language/spec>
o Dart 3 migration guide
<https://dart.dev/resources/dart-3-migration>
o Coming from ... <#sidenav-8-6>
+ JavaScript to Dart
<https://dart.dev/guides/language/coming-from/js-to-dart>
+ Swift to Dart
<https://dart.dev/guides/language/coming-from/swift-to-dart>
o Glossary <https://dart.dev/resources/glossary>
o Books <https://dart.dev/resources/books>
o Videos <https://dart.dev/resources/videos>
* Related sites <#sidenav-9>
o API referenceopen_in_new <https://api.dart.dev/>
o Blogopen_in_new <https://medium.com/dartlang>
o DartPad (online editor)open_in_new <https://dartpad.dev/>
o Flutteropen_in_new <https://flutter.dev/>
o Package siteopen_in_new <https://pub.dev/>
Contents
* Why use generics? <#why-use-generics>
* Using collection literals <#using-collection-literals>
* Using parameterized types with constructors
<#using-parameterized-types-with-constructors>
* Generic collections and the types they contain
<#generic-collections-and-the-types-they-contain>
* Restricting the parameterized type <#restricting-the-parameterized-type>
* Using generic methods <#using-generic-methods>
* ⟨ Collections <https://dart.dev/language/collections>
* Typedefs ⟩ <https://dart.dev/language/typedefs>
/description/
<https://github.com/dart-lang/site-www/tree/main/src/language/generics.md>
/bug_report/ <https://github.com/dart-lang/site-www/issues/new?
template=1_page_issue.yml&title=[PAGE%20ISSUE]:%20%27Generics%27&page-url=https://
dart.dev/language/generics.html&page-source=https://github.com/dart-lang/site-www/
tree/main/src/language/generics.md>
Generics
Contents
* Why use generics? <#why-use-generics>
* Using collection literals <#using-collection-literals>
* Using parameterized types with constructors
<#using-parameterized-types-with-constructors>
* Generic collections and the types they contain
<#generic-collections-and-the-types-they-contain>
* Restricting the parameterized type <#restricting-the-parameterized-type>
* Using generic methods <#using-generic-methods>
If you look at the API documentation for the basic array type, |List|
<https://api.dart.dev/stable/dart-core/List-class.html>, you’ll see that
the type is actually |List<E>|. The <…> notation marks List as a
/generic/ (or /parameterized/) type—a type that has formal type
parameters. By convention
<https://dart.dev/effective-dart/design#do-follow-existing-mnemonic-conventions-
when-naming-type-parameters>, most type variables have single-letter names, such as
E, T, S, K, and V.
<#why-use-generics>Why use generics?
Generics are often required for type safety, but they have more benefits
than just allowing your code to run:
* Properly specifying generic types results in better generated code.
* You can use generics to reduce code duplication.
If you intend for a list to contain only strings, you can declare it as
|List<String>| (read that as “list of string”). That way you, your
fellow programmers, and your tools can detect that assigning a
non-string to the list is probably a mistake. Here’s an example:
|var names = <String>[];
names.addAll(['Seth', 'Kathy', 'Lars']);
names.add(42); // Error
|
Another reason for using generics is to reduce code duplication.
Generics let you share a single interface and implementation between
many types, while still taking advantage of static analysis. For
example, say you create an interface for caching an object:
|abstract class ObjectCache {
Object getByKey(String key);
void setByKey(String key, Object value);
}|
You discover that you want a string-specific version of this interface,
so you create another interface:
|abstract class StringCache {
String getByKey(String key);
void setByKey(String key, String value);
}|
Later, you decide you want a number-specific version of this interface…
You get the idea.
Generic types can save you the trouble of creating all these interfaces.
Instead, you can create a single interface that takes a type parameter:
|abstract class Cache<T> {
T getByKey(String key);
void setByKey(String key, T value);
}|
In this code, T is the stand-in type. It’s a placeholder that you can
think of as a type that a developer will define later.
<#using-collection-literals>Using collection literals
List, set, and map literals can be parameterized. Parameterized literals
are just like the literals you’ve already seen, except that you add
|</type/>| (for lists and sets) or |</keyType/, /valueType/>| (for maps)
before the opening bracket. Here is an example of using typed literals:
|var names = <String>['Seth', 'Kathy', 'Lars'];
var uniqueNames = <String>{'Seth', 'Kathy', 'Lars'};
var pages = <String, String>{
'index.html': 'Homepage',
'robots.txt': 'Hints for web robots',
'humans.txt': 'We are people, not machines'
};|
<#using-parameterized-types-with-constructors>Using parameterized
types with constructors
To specify one or more types when using a constructor, put the types in
angle brackets (|<...>|) just after the class name. For example:
|var nameSet = Set<String>.from(names);|
The following code creates a map that has integer keys and values of
type View:
|var views = Map<int, View>();|
<#generic-collections-and-the-types-they-contain>Generic collections
and the types they contain
Dart generic types are /reified/, which means that they carry their type
information around at runtime. For example, you can test the type of a
collection:
|var names = <String>[];
names.addAll(['Seth', 'Kathy', 'Lars']);
print(names is List<String>); // true|
/info/ *Note:* In contrast, generics in Java use /erasure/, which means
that generic type parameters are removed at runtime. In Java, you can
test whether an object is a List, but you can’t test whether it’s a
|List<String>|.
<#restricting-the-parameterized-type>Restricting the parameterized type
When implementing a generic type, you might want to limit the types that
can be provided as arguments, so that the argument must be a subtype of
a particular type. You can do this using |extends|.
A common use case is ensuring that a type is non-nullable by making it a
subtype of |Object| (instead of the default, |Object?|
<https://dart.dev/null-safety/understanding-null-safety#top-and-bottom>).
|class Foo<T extends Object> {
// Any type provided to Foo for T must be non-nullable.
}|
You can use |extends| with other types besides |Object|. Here’s an
example of extending |SomeBaseClass|, so that members of |SomeBaseClass|
can be called on objects of type |T|:
|class Foo<T extends SomeBaseClass> {
// Implementation goes here...
String toString() => "Instance of 'Foo<$T>'";
}
class Extender extends SomeBaseClass {...}|
It’s OK to use |SomeBaseClass| or any of its subtypes as the generic
argument:
|var someBaseClassFoo = Foo<SomeBaseClass>();
var extenderFoo = Foo<Extender>();|
It’s also OK to specify no generic argument:
|var foo = Foo();
print(foo); // Instance of 'Foo<SomeBaseClass>'|
Specifying any non-|SomeBaseClass| type results in an error:
|var foo = Foo<Object>();|
<#using-generic-methods>Using generic methods
Methods and functions also allow type arguments:
|T first<T>(List<T> ts) {
// Do some initial work or error checking, then...
T tmp = ts[0];
// Do some additional checking or processing...
return tmp;
}|
Here the generic type parameter on |first| (|<T>|) allows you to use the
type argument |T| in several places:
* In the function’s return type (|T|).
* In the type of an argument (|List<T>|).
* In the type of a local variable (|T tmp|).
* ⟨ Collections <https://dart.dev/language/collections>
* Typedefs ⟩ <https://dart.dev/language/typedefs>
Dart <https://dart.dev/>
<https://medium.com/dartlang> <https://github.com/dart-lang>
<https://twitter.com/dart_lang>
Except as otherwise noted, this site is licensed under a Creative
Commons Attribution 4.0 International License
<https://creativecommons.org/licenses/by/4.0/>, and code samples are
licensed under the 3-Clause BSD License
<https://opensource.org/licenses/BSD-3-Clause>.
* Terms <https://dart.dev/terms>
* Privacy <https://policies.google.com/privacy>
* Security <https://dart.dev/security>