October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Swap Two Variables in Dart Without a Temporary Variable

Dart 3.0+ lets you swap two local variables with the record assignment pattern `(b, a) = (a, b);`—no third temporary variable required.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Dart 3.0 or later, swap two local variables with a record assignment pattern: (b, a) = (a, b);. The right side carries their current values, and the left-side pattern assigns them in reverse order.

Swap two local variables with a record pattern

Here is the complete example from the official Dart Patterns guide:

As an Amazon Associate I earn from qualifying purchases.

var (a, b) = ('left', 'right');
(b, a) = (a, b);
print('$a $b'); // right left

The first line declares and initializes a and b. The second line is an assignment to those existing variables, not a declaration of new ones. The record on the right groups their current values; the pattern on the left assigns the positional values to the variables in the opposite order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Requirements and scope

Dart language version 3.0 or later

Records and patterns require a Dart language version of at least 3.0. If a project is configured for an earlier language version, this syntax is unavailable under that configuration. See the official Dart Records guide and Patterns guide.

Targets must be local variables

Pattern assignment cannot directly target a field or a top-level variable. The analyzer documents this restriction in pattern_assignment_not_local_variable. For a field, first use local variables in the pattern assignment, then assign the desired value to the field separately.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why this works

A Dart record is a fixed-size, typed aggregate value that can hold values of different types. Its fields have no setters, but the record can carry both current values into an assignment pattern. The pattern assigns those values to the existing local variables; it does not mutate the record.

No arithmetic trick is required. This is a concise language feature, not evidence of a memory saving or a speed improvement; the cited Dart documentation makes no such performance claim.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.