Dart Extension Methods: Adding Methods to Existing Types You Don't Own
A tutorial on Dart extensions: declare extension on String, List, or any type, resolve API conflicts, and write generic extensions.
Published on • December 4, 2026
AI Assistant

int.parse('42') works, but it reads backwards: the type you’re converting to leads, and the value you’re converting from trails. What you really want is '42'.parseInt() — the call site reads left to right, and IDE completion offers it right alongside String’s built-in members.
The catch: you can’t edit String. It ships with the SDK, and it’s not yours. Dart’s extension methods solve exactly this — they add functionality to existing libraries and types without modifying them or subclassing them. You import a library, and suddenly '42'.parseInt() exists.
This post is a step-by-step guide to extensions: declaring them, using them, the static-type rule that catches everyone once, resolving API conflicts, and writing generic extensions over List<T> and beyond.
1. Declaring an extension
The syntax is an extension declaration with an on type:
// lib/string_apis.dart
extension NumberParsing on String {
int parseInt() {
return int.parse(this); // `this` is the receiver, e.g. '42'.
}
double parseDouble() {
return double.parse(this);
}
}
In shorthand:
extension <extension name>? on <type> { // name is optional
<member definition>* // one or more members
}
Members inside an extension can be:
| Member kind | Allowed? |
|---|---|
| Methods | Yes |
| Getters / setters | Yes |
| Operators | Yes |
| Static fields and static helper methods | Yes |
| Instance fields | No — extensions add behavior, not state |
Access static members outside the declaration through its name, the same way you’d access class-level members: NumberParsing.someStatic.
2. Using an extension: just import it
Extension methods live in libraries like all other Dart code. Import the library and use them like ordinary methods:
import 'string_apis.dart';
void main() {
print('42'.padLeft(5)); // A regular String method: ' 42'.
print('42'.parseInt()); // An extension method: 42.
}
That’s the whole usage model. Code completion in your IDE suggests extension members alongside regular ones — which is why you might use them without knowing it.
3. The static type rule (the one gotcha)
Extension methods are resolved against the static type of the receiver. That has one sharp consequence: you can’t invoke extension methods on variables of type dynamic:
dynamic d = '2';
print(d.parseInt()); // Runtime exception: NoSuchMethodError
Why? With dynamic, the call is dispatched at runtime, and the runtime has no idea an extension exists — extensions are a compile-time, static-resolution feature. The flip side is good news: because resolution is static, extension calls are as fast as calling a static function.
Type inference keeps things working in the normal case:
var v = '2';
print(v.parseInt()); // Works: `v` is inferred as String, output 2.
Since var v = '2' gives v the static type String, the extension resolves fine. Only an explicit dynamic (or an untyped parameter that’s dynamic) breaks the chain.
Related: for how inference decides these static types, see the type inference post.
4. Resolving API conflicts
What happens when two libraries both define parseInt() on String, or an extension member collides with an interface member? You have three tools.
Option A: show and hide on the import
// Defines the String extension method parseInt().
import 'string_apis.dart';
// Also defines parseInt(), but hiding NumberParsing2 hides it.
import 'string_apis_2.dart' hide NumberParsing2;
// print('42'.parseInt()); // Uses the parseInt() from string_apis.dart.
Option B: apply the extension explicitly
Named extensions can be applied directly, which reads like wrapping the value:
import 'string_apis.dart'; // NumberParsing extension.
import 'string_apis_2.dart'; // NumberParsing2 extension.
void main() {
// print('42'.parseInt()); // Doesn't work: ambiguous.
print(NumberParsing('42').parseInt());
print(NumberParsing2('42').parseInt());
}
Option C: import with a prefix
If both libraries define an extension with the same name, use a prefix:
import 'string_apis.dart';
import 'string_apis_3.dart' as rad;
void main() {
// print('42'.parseInt()); // Doesn't work: name conflict.
print(NumberParsing('42').parseInt()); // From string_apis.dart.
print(rad.NumberParsing('42').parseInt()); // From string_apis_3.dart.
print('42'.parseNum()); // Only string_apis_3.dart has parseNum().
}
Note the subtlety the docs point out: implicit invocation ('42'.parseNum()) works even with prefixed imports — you only need the prefix when applying an extension explicitly to dodge a name clash.
5. Unnamed extensions
The name in extension NumberParsing on String is optional. Unnamed extensions are visible only in the library where they’re declared, and — having no name — can’t be explicitly applied to resolve conflicts:
extension on String {
bool get isBlank => trim().isEmpty;
}
void main() {
print(' '.isBlank); // true — fine within this library.
}
Two constraints follow:
- You can invoke an unnamed extension’s static members only within the extension declaration itself.
- Since they’re library-private, unnamed extensions are ideal for local helpers you don’t want leaking into your public API.
6. Generic extensions
Extensions can have type parameters — useful for adding members to generic types like List<T>:
extension MyFancyList<T> on List<T> {
int get doubleLength => length * 2;
List<T> operator -() => reversed.toList();
List<List<T>> split(int at) => [sublist(0, at), sublist(at)];
}
void main() {
final words = ['a', 'b', 'c', 'd'];
print(words.doubleLength); // 8.
print(-words); // ['d', 'c', 'b', 'a'].
print(words.split(2)); // [['a', 'b'], ['c', 'd']].
}
T is bound from the static type of the receiver, so words.split(2) infers T = String and returns List<List<String>>.
7. Extensions vs. the alternatives
Before you reach for an extension, confirm it’s the right tool:
| Goal | Tool | Modifies type? | Notes |
|---|---|---|---|
| Add a method to a type you do own | Regular class member | Yes | Simplest option |
Add a method to a type you don’t own (String, DateTime, third-party) | extension | No | Static resolution only |
| Change how a type is represented/converted at compile time | extension type | No | Different feature — see extension types |
| Reuse behavior across hierarchies | mixin | No | Requires a class to apply to |
| Add members to a hierarchy you’re extending | extends | Creates subtype | Only for true is-a relationships |
Two more facts worth knowing:
- Extension members never participate in
dynamicdispatch — there’s no runtime lookup for them, only static resolution against the receiver’s static type. - Class modifiers don’t apply to extensions.
abstract,base,final,interface, andsealedgovern class and mixin declarations; they don’t modifyextensiondeclarations.
The pattern to remember: define extensions where the behavior logically belongs, export them from a library, and consumers get fluent members on types they never controlled.
Checklist
- Use extensions when you need members on a type you don’t own; prefer plain members on your own classes.
- Remember extensions are resolved against the static type — extension calls on
dynamicthrowNoSuchMethodErrorat runtime. - Don’t declare instance fields in an extension; use methods, getters, setters, operators, and static members only.
- Name every extension that might be imported alongside another — unnamed extensions are library-private and can’t disambiguate conflicts.
- Resolve collisions with
show/hide, explicit application (NumberParsing('42').parseInt()), or import prefixes. - When applying an extension explicitly, prefix it if two libraries use the same extension name.
- Parameterize generic extensions (
extension X<T> on List<T>) so inference bindsTfrom the receiver. - Import the extension’s library — extension members aren’t visible transitively.
- Reach for
extension typeinstead when you need compile-time representation changes rather than added members.
Conclusion
Extension methods let you shape an API you didn’t write — turning int.parse('42') into '42'.parseInt() without touching the String class. The model is deliberately simple: declare extension ... on Type, import the library, and call members as if they were always there.
The one rule to internalize is static resolution: extensions never dispatch on dynamic, which is also why they’re as fast as static functions. Master that, plus the three conflict-resolution tools, and extensions become a safe, everyday part of your Dart toolkit.