mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-28 17:08:29 +01:00
12 KiB
12 KiB
wger Flutter app
Flutter (SDK 3.10+) with flutter_riverpod v3, drift, freezed + json_serializable for codegen.
Verify after edits
flutter analyze— must be zero issuesflutter test— or scope:flutter test test/<feature>_test.dart- After touching anything with
@freezed/@JsonSerializable/ drift annotations:dart run build_runner build --delete-conflicting-outputs, thendart format .. A rebuild always rewrites the riverpod debug hashes inapp_settings_notifier.g.dart,auth_notifier.g.dart,powersync.g.dartandhealth_sync.g.dart; revert those unless you touched their source.
Layout
lib/
├── features/<feature>/ feature domains, named after the Django app in wger/server/wger/<app>/ (same names as wger-react)
├── core/ cross-cutting infrastructure and the app shell
│ ├── network/ wger_base, base_provider, the whole session/token layer (auth_notifier, auth_http_client, ...)
│ ├── widgets/ generic widgets (DecimalInputWidget, FormSubmitButton, ConfirmDeleteDialog, ...)
│ ├── widgets/dashboard/ dashboard cards, composes every feature (dashboard is not a feature)
│ ├── formatting/, exceptions/
│ └── *.dart helpers (date, validators, snackbar, ...) and shell screens (home_tabs, splash, update_*)
├── database/ the single Drift AppDatabase, can't be split per feature
│ ├── powersync/tables/ one file per domain, row classes via @UseRowClass
│ └── converters/ UtcDateTimeConverter, DateOnlyTextConverter, ...
├── powersync/ sync schema, upload connector, diagnostics
├── l10n/ app_*.arb + generated/, see lib/l10n/README.md
├── theme/
└── main.dart
Features: exercises, routines (Django manager, includes gym mode and logs), nutrition, measurements (also body
weight), gallery, trophies, account (user profile and settings, Django core), auth (login/MFA UI only, the
session layer is in core/network/), health (platform health sync, no Django app). Each feature uses this skeleton,
leaving out what it doesn't need and never inventing other top-level names:
features/<feature>/
├── models/ @freezed entities, filters, enums
├── providers/ notifiers AND repositories (<name>_repository.dart, <name>_notifier.dart)
├── screens/ full screens (<name>_screen.dart)
├── widgets/ UI fragments, forms under widgets/forms/ or widgets/<area>/
└── <name>.dart feature-wide helpers (validators.dart, gym_mode.dart), no helpers/ folder
core/or feature? Only promote tocore/when two unrelated features use it. When in doubt, keep it in the feature. Don't add newcore/→features/imports; the existing ones are the shell (dashboard, home tabs, app bar) and the logout wiring inauth_notifier.- Drift tables go into
database/powersync/tables/<domain>.dart, not into the feature; the model inmodels/provides thefromDbconstructor. - Codegen output (
*.g.dart,*.freezed.dart) sits next to its source and is committed. - Imports:
package:wger/...in new code; the few relative imports are leftovers, don't copy them. - Naming: snake_case files and folders, plural feature names.
test/
├── features/<feature>/..., core/, database/, powersync/ mirror lib/ one file per source file (<name>_test.dart)
├── helpers/ in_memory_drift, fake_auth_notifier, fake_auth_environment, ...
├── fixtures/ raw JSON API responses (fixture_reader.dart)
└── screenshots/ store screenshot scene builders
test_data/ shared Dart fixtures (getTestRoutine(), ...), structural variants stay in the test
integration_test/ screenshot driver
- Test file = source file +
_test, same path:lib/features/measurements/widgets/forms/entry.dartis tested intest/features/measurements/widgets/forms/entry_test.dart. One test file per source file, no merged or split files. *.mocks.dartare generated per test file by mockito (@GenerateMocks) and live next to it.
Stack-specific rules
- Riverpod:
- Notifiers go through a repository; no direct DB or
wgerBaseaccess in a notifier. - After mutations that change server state,
ref.invalidate(...)the affected provider(s). - Reading an
AsyncValuerequires handlingloadinganderror— don't shortcut with.value!. - Don't leak providers across screens — keep scope tight (
autoDisposewhere appropriate).
- Notifiers go through a repository; no direct DB or
- Decimal number inputs: never wire a raw
TextFormFieldstraight toNumberFormat.parse— keyboards can't be pinned to a locale, so a typed.in a,-locale is silently mis-read as grouping (80.5→805). UseDecimalInputWidget, or for a plain field addinputFormatters: [LocalizedDecimalInputFormatter(numberFormat.symbols.DECIMAL_SEP)]. - Codegen drift: if a
*.g.dart/*.freezed.dartis committed without the matching source change (or vice versa), the build is broken. Re-runbuild_runnerand stage both. // ignore:comments: don't add new ones to silence the analyzer. Fix the underlying issue. If genuinely unfixable, explain why in the same line.- Drift migrations: schema bumps need a
MigrationStrategythat handles existing data. Don't drop and recreate. - New columns on PowerSync-synced tables: there are no client-side migrations; the views regenerate for free, but pre-existing rows keep their old
ps_dataJSON and read NULL for the new column until re-downloaded (which only happens on a sync-rules change or a server-side backfill that touches the rows). So: make the Drift column.nullable()even when the domain is non-nullable, map NULL to defaults in afromDbconstructor wired via@UseRowClass(Model, constructor: 'fromDb')(model fields stay non-null, consumers untouched), and use NULL-safe SQL where the column is queried (isNotValue(x), notequals(x).not()). Keep the defaults permanently: installs that skip versions re-open the NULL window until their first full sync. Reference:lib/database/powersync/tables/measurements.dart+ the measurement models. - Date columns in Drift: a Django
DateTimeFieldcolumn needsUtcDateTimeConverter, aDateFieldcolumn needsDateOnlyTextConverter(never the other way round). A syncedDateFieldmust also be listed in_dateOnlyFieldsinlib/powersync/connector.dart, or the upload sends a full timestamp that Django rejects. - PowerSync column types: the
ps.Columntype must match the Drift column. Bool and int columns needps.Column.integer; behind aps.Column.texta bool reads'0'and Dart's'0' != 0turns every rowtrue. In-memory tests can't catch this. - REST write, local read: a REST write reaches the local DB only after the next sync. Don't
getByIdthe row right after a POST; use the response or a nullable lookup with a fallback. - Logs: never log a secret at any level (tokens, auth response bodies, deep-link URIs with a
#token=fragment). Logs are exported into public GitHub issues by the bug-report flow. - New language: a new
lib/l10n/app_<locale>.arbalso needs the locale inlocales_config.xml, iOSInfo.plistandlanguage_native_names.dart, seelib/l10n/README.md. - Create/edit forms: the form owns an editable
_draftof the entity, seeded once ininitState(widget.session ?? Entity(...defaults)); create vs edit follows from_draft.id == null. Never mutate the passed-in or stream-sourced object in place, and never build the default in the widget constructor (it re-runs every rebuild and drops in-progress edits). Mutate viacopyWith(entity models are@freezed). If the entity is fed from a stream and its identity can change under the form (e.g. a lazily created row arrives late), re-seed indidUpdateWidgetonly when theidchanges.SessionFormis the reference. - Text-field wiring: default to controllerless
TextFormField(initialValue: _draft.x, onSaved: (v) => _draft = _draft.copyWith(x: v)). Reach for aTextEditingControllerONLY when you need imperative access to the text (quick +/- buttons, autocomplete fill, clear button, format-while-typing, cross-field sync). A controller is state: create it ininitState, seed once,dispose()it; never as a field on aConsumerWidget/StatelessWidget, neverTextEditingController(...)insidebuild, nevercontroller.text =insidebuild.MeasurementCategoryFormis the reference for plain text fields,SessionForm's notes field for a justified controller.
Tests
- Prove a test can fail. After writing one, break the production side it guards (flip a type, delete the branch, drop a registry entry) and watch it go red, then restore. A test nobody has seen fail is a guess. This is how the suite grew tests that could never fail: a finder comparing element types against a widget type, an
every(...)without anexpect, acontains('10')that the prefilled100already satisfied, a file without the_testsuffix that the runner never collected. - Layering: repositories are tested against a real in-memory Drift DB (
test/helpers/in_memory_drift.dart), notifiers against a mocked repository (MockFooRepository, never a_FakeFoo extends Foo), widgets withauthProvider.overrideWith(() => FakeAuthNotifier(...))fromtest/helpers/fake_auth_notifier.dartinstead of seeding prefs and stubbing every auto-login request. One test file per source file, mirroring its path. - Test the clearing, not just the change. Every bug this rule would have caught came from a null crossing a boundary:
toCompanion()mapping null toValue.absent()(an update then leaves the column untouched), a hand-writtencopyWithwith?? this.xswallowing a cleared field. When a field becomes editable, assert non-null → null against the stored row, not only non-null → non-null. - Test the boundary day of a date range. Callers pass calendar days, entries carry timestamps, and the mismatch hides in the edges: an entry recorded during the end day, the day after, the start day. Same for anything counting days:
difference().inDaysmeasures elapsed time, so a range spanning the spring-forward switch comes up one day short. Count calendar days as UTC midnights. - Assert instants, never the local wall clock.
expect(converted, DateTime.parse('...11:15'))only holds in one zone; useisAtSameMomentAsagainst aDateTime.utc(...), or compare UTC components. CI pinsTZ=Europe/Berlin, so a local-time assumption stays invisible there and turns the suite red for everyone else. - Capture the payload on REST writes.
verify(base.post(any, uri))only pins the routing; a renamed or dropped field surfaces as a server-side 400. Useverify(base.post(captureAny, uri)).capturedand assert the wire names. - Structural fixture variants belong in the test, not in the shared corpus in
test_data/: growinggetTestRoutine()shifts expectations in dozens of files. Build the superset slot, the rest day, the empty plan locally. (This is why supersets had no coverage at all: the shared fixture defined what was testable.) - Widget tests:
tester.view.physicalSize/devicePixelRatioalways withaddTearDown(tester.view.reset), and every test sets the size it needs itself — two log-screen tests were silently living off the one a previous test left behind.installFakeAuthEnvironment()registers asetUp, so it goes at the top ofmain(); calling it inside anothersetUpthrows at runtime.
Scope
Make the minimum change requested. Don't refactor adjacent code, rename things, or move helpers unless explicitly asked. Mention adjacent issues as follow-ups.