Flutter tips that still compile.
30 short notes on Dart, widgets and Riverpod. Every snippet is real code, analyzed and tested against Flutter 3.47.5.
Last check: 27 Sept 2026
Flutter 3.47.5 · Dart 3.13.4 · 30/30 passing
query.dart✓ analyzed · tested
Map<String, String> queryParams({
required String q,
String? sort,
int? page,
}) {
return {
'q': q,
'sort': ?sort,
'page': ?page?.toString(),
};
}- ?value: the entry is skipped when sort is null
welcome_card.dart✓ analyzed · tested
return Container(
width: 240,
height: 120,
color: Colors.teal,
padding: const EdgeInsets.all(16),
alignment: Alignment.center,
child: const Text('Welcome'),
);- not const: Container has no const constructor
discount_badge.dart✓ analyzed · tested
if (percent == 0) {
return const SizedBox.shrink();
}
return Text('-$percent%');- const, zero by zero
stats.dart✓ analyzed · tested
(int, int) minMax(List<int> values) {
var low = values.first;
var high = values.first;
for (final v in values.skip(1)) {
if (v < low) low = v;
if (v > high) high = v;
}
return (low, high);
}- two values, one return type, no class
save_button.dart✓ analyzed · tested
return ElevatedButton(
onPressed: () async {
await save();
if (!context.mounted) return;
Navigator.of(context).pop();
},
child: const Text('Save'),
);- the widget may be gone by now
spacing.dart✓ analyzed · tested
return const Column(
spacing: 12,
children: [
Text('Mushrooms'),
Text('Olives'),
Text('Basil'),
],
);- one value instead of a SizedBox per gap
- no extra space after the last child
LatestRSS
- 030Check ref.mounted after an await in a providerA provider can be disposed while a Future is pending. Check ref.mounted after the await before you touch state, or Riverpod 3 throws.
- 029Style an ElevatedButton with styleFrom and themesUse ElevatedButton.styleFrom for one button, ElevatedButtonThemeData for all of them, and WidgetStateProperty when a style depends on state.
- 028Parse JSON safely with map patternsA switch on a map pattern checks keys and value types in one go, so bad JSON reaches your fallback case instead of a TypeError.
- 027Migrate a StateNotifier to a NotifierStateNotifier now lives in legacy.dart. Moving to Notifier takes a few mechanical steps, and one behaviour change is worth knowing about.
- 026Show real progress with a value on progress indicatorsGive CircularProgressIndicator or LinearProgressIndicator a value from 0.0 to 1.0 and they show how far along you are instead of spinning.
- 025Initialise async dependencies in main() and use requireValueAwait a FutureProvider once before runApp, then read it synchronously everywhere with requireValue. No loading states for things that are always ready.
Path · 7 tips
Cleaner widget code
Path · 8 tips