Test logic without a widget tree
Choose a model or service rule such as validating a draft order. Supply controlled inputs and compare the result with the required state. Keep expected values explicit and cover the boundaries that distinguish valid from invalid behavior, rather than testing only an ordinary success path that many incorrect implementations could also satisfy.
Place tests in the project's established test directory and use the configured Flutter or Dart test command. Match imports and dependencies to the repository. A unit case should not require rendering a widget when the rule can be called directly, because unnecessary framework setup makes a local failure less clear and slower to investigate.
Make asynchronous results deterministic
Await Future results or use the documented asynchronous matcher pattern so the runner observes completion and failures. Return a controlled value or error from a repository dependency. A live network request can make a local rule test depend on connectivity and remote data rather than the behavior named in the test.
For stream-driven logic, define the expected sequence and completion behavior. Avoid arbitrary delays as a substitute for observing emitted states. Reset subscriptions and created resources in cleanup so later cases do not receive events from an earlier scenario or retain mutable state that changes the meaning of their expected results.
Add widget and device checks for separate risks
Use widget tests when the requirement concerns a rendered message, input interaction, or component composition. A model test cannot show that a button is wired to the right action. Keep the distinction explicit so the logic suite remains focused and the interface suite provides evidence for connections rather than repeating every arithmetic input.
Use integration or device checks for platform plugins and complete journeys that depend on the runtime environment. A fake storage or camera adapter does not establish the real platform behavior. Record these limits and add the smallest appropriate broader check instead of treating one passing unit suite as comprehensive mobile application verification.
Working sheet
| Behavior | Layer | Example assertion |
|---|---|---|
| Validation rule | Unit | Invalid draft rejected |
| Async repository result | Unit with controlled dependency | Expected state returned |
| Error message rendering | Widget | Message visible |
| Camera upload journey | Device integration | Uploaded resource available |
Questions about write Flutter unit tests for Dart logic
Does a Flutter unit test render widgets?
A pure logic unit test does not need to. Use widget tests when rendering and interaction are the target.
Can a mocked plugin prove device behavior?
No. It can verify local handling of a controlled response. Real platform behavior needs appropriate integration evidence.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
