Skip to content

Lab · Flutter and Dart

Lab: a clinic's token desk

Labbeginner· about 1.5 hours· 4 steps

The brief

Model the reception desk of a small clinic in Dart: patients with optional details, a token desk that hands out numbers, general and specialist visits with a senior discount, and payment results handled with a sealed class and a switch.

Why it matters

Every app has models like these: a user whose phone number may be missing, a service that hides its counter, several kinds of bill, and a payment that can succeed, fail or wait. Clinic, salon and bank-queue apps in India run on exactly this kind of Dart, and sealed classes stop you forgetting a case.

Skills you practise

  • nullable types, ?. and ??
  • required named parameters
  • factory constructors and getters
  • private fields
  • extends, abstract classes and @override
  • mixins and extensions
  • enhanced enums
  • records and destructuring
  • sealed classes and switch patterns

You need

  • A web browser with DartPad (dartpad.dev), or the Dart SDK 3.5 or newer to run dart run clinic_desk.dart

What you will have at the end

clinic_desk.dart: patient, token desk, visit and payment models for a clinic, using null safety, constructors, inheritance, a mixin, an extension, an enum, a record and a sealed class.

How it is marked (rubric, 100 points)
Correct outputAll lines match: no null printed, tokens 1 to 3, Rs 560 for the senior, 2 paid visits worth Rs 860.45
Null safety used wellNullable types only where a value can really be missing; ?. and ?? instead of !; required on the fields that must be given.20
Good object designPrivate counter behind a method, a shared abstract Visit, a mixin and an extension each used for one clear job, and an exhaustive switch over the sealed class.25
ExplainedYou can say why the switch needs no default case and what the compiler does when a subtype is missing.10

0/4 steps done

Step 1 of 4

Patients with optional details

partOne prints both patients with "not given" and "self" in place of missing details, and a phone length of 0 for Ravi.

Give Patient a final String name and int age, both required named parameters, and two nullable fields: String? phone and String? referredBy. A ? on the type means the value may be null; without it Dart will not let null in at all.

Create two top-level patients: asha (Asha Patil, 34, phone '98765 43210') and ravi (Ravi Kumar, 61, referred by 'Dr. Mehta'). In partOne print each one, using ?? to put 'not given' and 'self' in place of null. For Ravi's phone length, ?. stops at null and ?? turns that null into 0.

Run this on your machine, then paste the output below
$ dart run clinic_desk.dart

Also check yourself

Your notes: clinic_desk.dart

Write down each command you ran. It goes into your project summary.