Dart Variables, Types and Collections With One App Example

A chef taps “Add one crate” twice on a box of tomatoes. The basket should show two crates on one line. If the chef saves the same tomatoes to their favorites twice, the favorites screen should still show one product. Same tomatoes, same number of taps, different results.

In Dart, a variable gives a value a name, a type describes what kind of value it can hold, and a collection groups values so you can work with them together. The chef’s order gives us a reason to use all three.

What is a variable in Dart?

Think of a paper order form. The space labeled “Number of crates” keeps its label when the chef crosses out one and writes two. In Dart, crateCount can play the same role. The name tells you what the value means, and you can refer to it again when the order changes.

var crateCount = 1;
crateCount = crateCount + 1;

The first line creates the variable. On the second line, Dart reads the current count, adds one, then assigns the result back to crateCount. Its value is now 2. The plus and equals signs are examples of operators you can use with variables.

From the initial value 1, Dart also worked out that the type is int. This is called type inference. Writing int crateCount = 1; makes the same type explicit.

Which Dart type fits the value?

Consider the information printed on one box. Several fields might contain digits, but what you need to do with each field determines its type.

  • String for text or an identifier. The product code '0042' keeps its leading zeros. Converting it to the number 42 would lose part of the code the warehouse uses.
  • int for whole numbers. A customer can add one crate to the existing two. Keeping the quantity as an integer lets Dart perform that addition.
  • double for measurements with a fractional part. A box can weigh 2.5 kilograms. Both double and int belong to the broader numeric type num.
  • bool for a yes-or-no answer. A field named requiresChilling can hold true or false.

Dart checks how you use these types. After var crateCount = 1;, assigning 'two' to that variable produces an analysis error. You may also encounter dynamic, which defers many checks until the program runs. With var and an integer starting value, Dart can catch the mistake as you write.

When should you use var, final or const?

Use var with an initial value when Dart can infer the type and you need to assign a new value later. Use final when the variable should be assigned only once. Its value can come from something you learn while the app is running, such as the product the customer just selected. Use const for a value known when the program is compiled.

const addButtonLabel = 'Add one crate';
final favoriteIds = <String>{};
favoriteIds.add('0042');

Why does the last line work? final keeps favoriteIds pointing to the same set. The set itself is mutable, so a customer can still add or remove favorites. A collection created with const has fixed contents.

How do you choose between List, Set and Map?

Use a List when position matters

A List keeps items in sequence and allows repeats. If the catalogue displays tomatoes followed by basil, ['0042', '0107'] captures that order. Dart counts positions from zero, so catalogueIds[0] reads the first item. After the chef sorts the catalogue, another product might occupy position zero. Use the product’s ID to remember what the chef selected.

Use a Set when membership matters

A Set holds each distinct value once. With a Set<String> of favorite product IDs, saving '0042' again leaves one entry. The screen can ask favoriteIds.contains('0042') to decide whether to show a filled star. To create an empty set of strings, write <String>{}. An empty {} on its own defaults to a map.

Use a Map when one value belongs to a key

A paper order has a product code on the left and a quantity on the right. A Map<String, int> represents that relationship. The first type describes the key, a product ID. The second describes the value, a crate count. Looking up basket['0042'] gets the count for tomatoes even if the catalogue has been sorted since the order began.

Try one order in DartPad

Paste this program into DartPad and select Run. Before you run it, predict the last two numbers. The chef saves the tomatoes twice and taps “Add one crate” twice.

void main() {
  const tomatoId = '0042';
  const basilId = '0107';
  final catalogueIds = [tomatoId, basilId];
  final favoriteIds = <String>{};
  final basket = <String, int>{};

favoriteIds.add(tomatoId); favoriteIds.add(tomatoId);

for (var tap = 0; tap < 2; tap++) { final currentCount = basket[tomatoId] ?? 0; basket[tomatoId] = currentCount + 1; }

print('First product: ${catalogueIds[0]}'); print('Saved products: ${favoriteIds.length}'); print('Tomato crates: ${basket[tomatoId]}'); }

First product: 0042
Saved products: 1
Tomato crates: 2

Now swap tomatoId and basilId inside the catalogue list and run it again. The first line changes to First product: 0107. The chef still has two crates of tomatoes. This small test shows why a basket should follow a product’s ID when the screen changes order.

When does a missing value mean zero?

On the first tap, the basket has no entry for tomatoes. A Dart map lookup returns null for a missing key. The expression basket[tomatoId] ?? 0 means “use the stored count, or start at zero if there is no entry.” That fits the basket because the chef has added zero crates so far.

The same shortcut could mislead the chef on a stock screen. If the warehouse has yet to report how many crates are available, a zero would read as “sold out.” A variable of type int? can hold either a whole number or null. That gives the screen enough information to show “Stock not confirmed” until a count arrives. A confirmed count of 0 can then mean exactly what it says.

How this applies to a mobile ordering app

As Technical Lead at Apps Panel / Nomeo, I worked on a food wholesaler’s app that let professional customers browse a catalogue, place orders and track them. It also synchronized with the wholesaler’s existing system. The tomato basket is a teaching example inspired by that workflow.

For an ordering MVP, one of the most revealing tests is to add two crates, change the catalogue sort order and reopen the basket. The right product and quantity should still be there. If you’re planning a mobile app around an existing catalogue, that is the kind of detail I can help you work through before launch. Explore my mobile and backend expertise.