import 'package:material_ui/material_ui.dart'; void main() => runApp(const MaterialApp(home: OracleScreen())); sealed class Mood {} final class Asleep extends Mood {} final class Thinking extends Mood {} final class Spoke extends Mood { Spoke(this.answer); final String answer; } final class Confused extends Mood { Confused(this.reason); final String reason; } const answers = [ 'Yes.', 'No.', 'Ask again after lunch.', 'The signs point to maybe.', ]; Future consult(int question) async { await Future.delayed(const Duration(seconds: 2)); if (question % 5 == 0) { throw Exception('The smoke cleared too early.'); } return answers[(question - 1) % answers.length]; } class OracleScreen extends StatefulWidget { const OracleScreen({super.key}); @override State createState() => _OracleScreenState(); } class _OracleScreenState extends State { Mood _mood = Asleep(); int _asked = 0; Future _ask() async { setState(() { _mood = Thinking(); _asked++; }); Mood next; try { next = Spoke(await consult(_asked)); } catch (e) { next = Confused('$e'); } if (!mounted) return; setState(() => _mood = next); } @override Widget build(BuildContext context) { final thinking = _mood is Thinking; return Scaffold( body: Center( child: Column( mainAxisAlignment: .center, spacing: 24, children: [ Text(switch (_mood) { Asleep() => 'The oracle is asleep.', Thinking() => 'The oracle is thinking…', Spoke(:final answer) => answer, Confused(:final reason) => 'The oracle is confused. $reason', }, style: Theme.of(context).textTheme.headlineSmall), if (thinking) const CircularProgressIndicator(), FilledButton( onPressed: thinking ? null : _ask, child: Text(_asked == 0 ? 'Ask' : 'Ask again'), ), Text('Questions asked: $_asked'), ], ), ), ); } } // Try this next… // // Delete `if (!mounted) return;` and run `flutter test`. One test fails, and // the message names the mistake. Read it: it says what happened, and why the // line was there. // // Change the last line of _ask to a plain `_mood = next;`, with no setState. // Run the app and ask a question. The oracle answers, but the screen stays // on "thinking" and the button stays off, because nothing asked Flutter to // call build again. Now press `r` in the terminal. Hot reload reruns build, // and the answer was there all along. // // Add a fifth Mood, say `Sulking`, and run `flutter analyze`. The analyzer // points at the switch in build and tells you which case you forgot. // // Tap "Ask again" twice, fast. You can't: the button is disabled while the // oracle thinks, because build reads `thinking`. Now always pass `_ask` to // onPressed, tap twice, and watch two answers race. Which one wins? How would // you make sure only the newest question's answer counts? // // Replace the Thinking state and the await with a FutureBuilder, a widget // that rebuilds itself when a Future finishes. Which version is easier to // read? Which one could you test? // // In a `flutter run` session, press `h` to see every key the terminal // answers to. Try `p`, and see the outline of every widget build described.