@bdayadev/flutter-ultra-mcp 1.12.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +41 -13
- package/dart/ultra_flutter/pubspec.yaml +1 -1
- package/dart/ultra_flutter_devtools/pubspec.yaml +1 -1
- package/package.json +1 -1
- package/packages/flutter-ultra-native-desktop/package.json +2 -0
- package/packages/flutter-ultra-native-mobile/package.json +2 -0
- package/skills/add-integration-test/SKILL.md +72 -20
- package/skills/add-unit-test/SKILL.md +1 -0
- package/skills/add-widget-preview/SKILL.md +75 -26
- package/skills/add-widget-test/SKILL.md +51 -22
- package/skills/apply-architecture-best-practices/SKILL.md +55 -29
- package/skills/build-cli-app/SKILL.md +36 -9
- package/skills/build-responsive-layout/SKILL.md +65 -31
- package/skills/collect-coverage/SKILL.md +12 -1
- package/skills/debug/SKILL.md +19 -2
- package/skills/drive/SKILL.md +30 -15
- package/skills/fix-layout-issues/SKILL.md +71 -20
- package/skills/fix-runtime-errors/SKILL.md +79 -20
- package/skills/generate-test-mocks/SKILL.md +43 -13
- package/skills/implement-json-serialization/SKILL.md +82 -17
- package/skills/migrate-to-checks-package/SKILL.md +46 -14
- package/skills/record-demo/SKILL.md +20 -0
- package/skills/resolve-package-conflicts/SKILL.md +41 -12
- package/skills/run-static-analysis/SKILL.md +29 -16
- package/skills/setup/SKILL.md +1 -0
- package/skills/setup-declarative-routing/SKILL.md +166 -30
- package/skills/setup-localization/SKILL.md +90 -20
- package/skills/test/SKILL.md +2 -1
- package/skills/tour/SKILL.md +31 -22
- package/skills/use-http-package/SKILL.md +61 -23
- package/skills/use-pattern-matching/SKILL.md +63 -26
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fix-layout-issues
|
|
3
3
|
description: Fixes Flutter layout errors (overflows, unbounded constraints) using Dart and Flutter MCP tools. Use when addressing "RenderFlex overflowed", "Vertical viewport was given unbounded height", or similar layout issues.
|
|
4
|
+
last_modified: Tue, 21 Apr 2026 19:45:59 GMT
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
# Resolving Flutter Layout Errors
|
|
@@ -13,35 +14,40 @@ description: Fixes Flutter layout errors (overflows, unbounded constraints) usin
|
|
|
13
14
|
|
|
14
15
|
## Constraint Violation Diagnostics
|
|
15
16
|
|
|
16
|
-
Flutter layout operates on: **Constraints go down. Sizes go up. Parent sets position.** Layout errors occur when this negotiation fails.
|
|
17
|
+
Flutter layout operates on a strict rule: **Constraints go down. Sizes go up. Parent sets position.** Layout errors occur when this negotiation fails, typically due to unbounded constraints or unconstrained children.
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
- **"
|
|
21
|
-
- **"
|
|
22
|
-
- **"
|
|
19
|
+
Diagnose layout failures using the following error signatures:
|
|
20
|
+
|
|
21
|
+
- **"Vertical viewport was given unbounded height"**: Triggered when a scrollable widget (`ListView`, `GridView`) is placed inside an unconstrained vertical parent (`Column`). The parent provides infinite height, and the child attempts to expand infinitely.
|
|
22
|
+
- **"An InputDecorator...cannot have an unbounded width"**: Triggered when a `TextField` or `TextFormField` is placed inside an unconstrained horizontal parent (`Row`). The text field attempts to determine its width based on infinite available space.
|
|
23
|
+
- **"RenderFlex overflowed"**: Triggered when a child of a `Row` or `Column` requests a size larger than the parent's allocated constraints. Visually indicated by yellow and black warning stripes.
|
|
24
|
+
- **"Incorrect use of ParentData widget"**: Triggered when a `ParentDataWidget` is not a direct descendant of its required ancestor. (e.g., `Expanded` outside a `Flex`, `Positioned` outside a `Stack`).
|
|
25
|
+
- **"RenderBox was not laid out"**: A cascading side-effect error. Ignore this and look further up the stack trace for the primary constraint violation (usually an unbounded height/width error).
|
|
23
26
|
|
|
24
27
|
## Layout Error Resolution Workflow
|
|
25
28
|
|
|
29
|
+
Copy and use this checklist to systematically resolve layout constraint violations.
|
|
30
|
+
|
|
26
31
|
### Task Progress
|
|
27
32
|
|
|
28
|
-
- [ ] Run the application in debug mode to capture the exception.
|
|
29
|
-
- [ ] Identify the primary error message (ignore cascading errors).
|
|
30
|
-
- [ ] Apply the conditional fix:
|
|
31
|
-
- **"
|
|
32
|
-
- **"
|
|
33
|
-
- **"RenderFlex overflowed"**:
|
|
34
|
-
- **"Incorrect ParentData"**: Move the
|
|
33
|
+
- [ ] Run the application in debug mode to capture the exact layout exception in the console.
|
|
34
|
+
- [ ] Identify the primary error message (ignore cascading "RenderBox was not laid out" errors).
|
|
35
|
+
- [ ] Apply the conditional fix based on the specific error type:
|
|
36
|
+
- **If "Vertical viewport was given unbounded height"**: Wrap the scrollable child (`ListView`, `GridView`) in an `Expanded` widget to consume remaining space, or wrap it in a `SizedBox` to provide an absolute height constraint.
|
|
37
|
+
- **If "An InputDecorator...cannot have an unbounded width"**: Wrap the `TextField` or `TextFormField` in an `Expanded` or `Flexible` widget.
|
|
38
|
+
- **If "RenderFlex overflowed"**: Constrain the overflowing child by wrapping it in an `Expanded` widget (to force it to fit) or a `Flexible` widget (to allow it to be smaller than the allocated space).
|
|
39
|
+
- **If "Incorrect use of ParentData widget"**: Move the `ParentDataWidget` to be a direct child of its required parent. Ensure `Expanded`/`Flexible` are direct children of `Row`/`Column`/`Flex`. Ensure `Positioned` is a direct child of `Stack`.
|
|
35
40
|
- [ ] Execute Flutter hot reload.
|
|
36
|
-
- [ ]
|
|
41
|
+
- [ ] Run validator -> review errors -> fix: Inspect the UI to verify the red/grey error screen or yellow/black overflow stripes are resolved. If new layout errors appear, repeat the workflow.
|
|
37
42
|
|
|
38
43
|
## Examples
|
|
39
44
|
|
|
40
45
|
### Fixing Unbounded Height (ListView in Column)
|
|
41
46
|
|
|
42
|
-
**
|
|
47
|
+
**Input (Error State):**
|
|
43
48
|
|
|
44
49
|
```dart
|
|
50
|
+
// Throws "Vertical viewport was given unbounded height"
|
|
45
51
|
Column(
|
|
46
52
|
children: <Widget>[
|
|
47
53
|
const Text('Header'),
|
|
@@ -55,9 +61,10 @@ Column(
|
|
|
55
61
|
)
|
|
56
62
|
```
|
|
57
63
|
|
|
58
|
-
**
|
|
64
|
+
**Output (Resolved State):**
|
|
59
65
|
|
|
60
66
|
```dart
|
|
67
|
+
// Wrap ListView in Expanded to constrain its height to the remaining Column space
|
|
61
68
|
Column(
|
|
62
69
|
children: <Widget>[
|
|
63
70
|
const Text('Header'),
|
|
@@ -75,15 +82,59 @@ Column(
|
|
|
75
82
|
|
|
76
83
|
### Fixing Unbounded Width (TextField in Row)
|
|
77
84
|
|
|
78
|
-
**
|
|
85
|
+
**Input (Error State):**
|
|
86
|
+
|
|
87
|
+
```dart
|
|
88
|
+
// Throws "An InputDecorator...cannot have an unbounded width"
|
|
89
|
+
Row(
|
|
90
|
+
children: [
|
|
91
|
+
const Icon(Icons.search),
|
|
92
|
+
TextField(),
|
|
93
|
+
],
|
|
94
|
+
)
|
|
95
|
+
```
|
|
79
96
|
|
|
80
|
-
**
|
|
97
|
+
**Output (Resolved State):**
|
|
98
|
+
|
|
99
|
+
```dart
|
|
100
|
+
// Wrap TextField in Expanded to constrain its width to the remaining Row space
|
|
101
|
+
Row(
|
|
102
|
+
children: [
|
|
103
|
+
const Icon(Icons.search),
|
|
104
|
+
Expanded(
|
|
105
|
+
child: TextField(),
|
|
106
|
+
),
|
|
107
|
+
],
|
|
108
|
+
)
|
|
109
|
+
```
|
|
81
110
|
|
|
82
111
|
### Fixing RenderFlex Overflow
|
|
83
112
|
|
|
84
|
-
**
|
|
113
|
+
**Input (Error State):**
|
|
114
|
+
|
|
115
|
+
```dart
|
|
116
|
+
// Throws "A RenderFlex overflowed by X pixels on the right"
|
|
117
|
+
Row(
|
|
118
|
+
children: [
|
|
119
|
+
const Icon(Icons.info),
|
|
120
|
+
const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
|
121
|
+
],
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Output (Resolved State):**
|
|
85
126
|
|
|
86
|
-
|
|
127
|
+
```dart
|
|
128
|
+
// Wrap the Text widget in Expanded to force it to wrap within the available constraints
|
|
129
|
+
Row(
|
|
130
|
+
children: [
|
|
131
|
+
const Icon(Icons.info),
|
|
132
|
+
Expanded(
|
|
133
|
+
child: const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
|
134
|
+
),
|
|
135
|
+
],
|
|
136
|
+
)
|
|
137
|
+
```
|
|
87
138
|
|
|
88
139
|
## Flutter Ultra Integration
|
|
89
140
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fix-runtime-errors
|
|
3
3
|
description: Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
|
|
4
|
+
last_modified: Fri, 24 Apr 2026 15:13:22 GMT
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
# Resolving Dart Static Analysis Errors
|
|
@@ -8,7 +9,11 @@ description: Uses get_runtime_errors and lsp to fetch an active stack trace, loc
|
|
|
8
9
|
## Contents
|
|
9
10
|
|
|
10
11
|
- [Core Concepts & Guidelines](#core-concepts--guidelines)
|
|
12
|
+
- [Type System & Soundness](#type-system--soundness)
|
|
13
|
+
- [Null Safety](#null-safety)
|
|
14
|
+
- [Error Handling](#error-handling)
|
|
11
15
|
- [Workflows](#workflows)
|
|
16
|
+
- [Workflow: Static Analysis Resolution](#workflow-static-analysis-resolution)
|
|
12
17
|
- [Examples](#examples)
|
|
13
18
|
|
|
14
19
|
## Core Concepts & Guidelines
|
|
@@ -18,30 +23,32 @@ description: Uses get_runtime_errors and lsp to fetch an active stack trace, loc
|
|
|
18
23
|
Enforce Dart's sound type system to prevent runtime invalid states.
|
|
19
24
|
|
|
20
25
|
- **Method Overrides:** Maintain sound return types (covariant) and parameter types (contravariant). Never tighten a parameter type in a subclass unless explicitly marked with the `covariant` keyword.
|
|
21
|
-
- **Generics & Collections:** Add explicit type annotations to generic classes (e.g., `List<T>`, `Map<K, V>`). Never assign a `List<dynamic>` to a typed list.
|
|
26
|
+
- **Generics & Collections:** Add explicit type annotations to generic classes (e.g., `List<T>`, `Map<K, V>`). Never assign a `List<dynamic>` to a typed list (e.g., `List<Cat>`).
|
|
22
27
|
- **Downcasting:** Avoid implicit downcasts from `dynamic`. Use explicit casts (e.g., `as List<Cat>`) when necessary, but ensure the underlying runtime type matches to prevent `TypeError` exceptions.
|
|
23
|
-
- **Strict Casts:** Enable `strict-casts: true` in `analysis_options.yaml` under `analyzer: language:` to force explicit casting.
|
|
28
|
+
- **Strict Casts:** Enable `strict-casts: true` in `analysis_options.yaml` under `analyzer: language:` to force explicit casting and catch implicit downcast errors at compile time.
|
|
24
29
|
|
|
25
30
|
### Null Safety
|
|
26
31
|
|
|
27
32
|
Eliminate static errors related to null safety by correctly managing variable initialization and nullability.
|
|
28
33
|
|
|
29
34
|
- **Modifiers:** Apply `?` for nullable types, `!` for null assertions, and `required` for named parameters that cannot be null.
|
|
30
|
-
- **Late Initialization:** Use the `late` keyword for non-nullable variables guaranteed to be initialized before use.
|
|
31
|
-
- **Wildcards:** Use the `_` wildcard variable (Dart 3.7+) for non-binding local variables or parameters.
|
|
35
|
+
- **Late Initialization:** Use the `late` keyword for non-nullable variables guaranteed to be initialized before use. Apply this specifically to top-level or instance variables where Dart's control flow analysis cannot definitively prove initialization.
|
|
36
|
+
- **Wildcards:** Use the `_` wildcard variable (Dart 3.7+) for non-binding local variables or parameters to avoid unused variable warnings.
|
|
32
37
|
|
|
33
38
|
### Error Handling
|
|
34
39
|
|
|
35
40
|
Distinguish between recoverable exceptions and unrecoverable errors.
|
|
36
41
|
|
|
37
42
|
- **Catching:** Catch `Exception` subtypes for recoverable failures.
|
|
38
|
-
- **Errors:** Never explicitly catch `Error` or its subtypes (e.g., `TypeError`, `ArgumentError`). Errors indicate programming bugs that must be fixed, not caught.
|
|
43
|
+
- **Errors:** Never explicitly catch `Error` or its subtypes (e.g., `TypeError`, `ArgumentError`). Errors indicate programming bugs that must be fixed, not caught. Enforce this by enabling the `avoid_catching_errors` linter rule.
|
|
39
44
|
- **Rethrowing:** Use `rethrow` inside a `catch` block to propagate an exception while preserving its original stack trace.
|
|
40
45
|
|
|
41
46
|
## Workflows
|
|
42
47
|
|
|
43
48
|
### Workflow: Static Analysis Resolution
|
|
44
49
|
|
|
50
|
+
Use this sequential workflow to identify, fix, and verify static analysis errors in a Dart project. Copy the checklist to track your progress.
|
|
51
|
+
|
|
45
52
|
**Task Progress:**
|
|
46
53
|
|
|
47
54
|
- [ ] 1. Run static analyzer.
|
|
@@ -50,36 +57,52 @@ Distinguish between recoverable exceptions and unrecoverable errors.
|
|
|
50
57
|
- [ ] 4. Verify fixes (Feedback Loop).
|
|
51
58
|
|
|
52
59
|
**1. Run static analyzer**
|
|
60
|
+
Execute the Dart analyzer to identify all static errors in the target directory or file.
|
|
53
61
|
|
|
54
62
|
```bash
|
|
55
63
|
dart analyze . --fatal-infos
|
|
56
64
|
```
|
|
57
65
|
|
|
58
66
|
**2. Apply automated fixes**
|
|
67
|
+
Use the `dart fix` tool to automatically resolve standard linting and analysis issues.
|
|
59
68
|
|
|
60
69
|
```bash
|
|
70
|
+
# Preview changes
|
|
61
71
|
dart fix --dry-run
|
|
72
|
+
# Apply changes
|
|
62
73
|
dart fix --apply
|
|
63
74
|
```
|
|
64
75
|
|
|
65
76
|
**3. Resolve remaining errors manually**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
- **If
|
|
69
|
-
-
|
|
77
|
+
Review the remaining analyzer output and apply conditional logic based on the error type:
|
|
78
|
+
|
|
79
|
+
- **If the error is a Null Safety issue (e.g., "Property cannot be accessed on a nullable receiver"):**
|
|
80
|
+
- Verify if the variable can logically be null.
|
|
81
|
+
- If yes, use optional chaining (`?.`) or provide a fallback (`??`).
|
|
82
|
+
- If no, and initialization is guaranteed elsewhere, mark the declaration with `late`.
|
|
83
|
+
- **If the error is a Type Mismatch (e.g., "The argument type 'List<dynamic>' can't be assigned..."):**
|
|
84
|
+
- Trace the variable's initialization.
|
|
85
|
+
- Add explicit generic type annotations to the instantiation (e.g., `<int>[]` instead of `[]`).
|
|
86
|
+
- **If the error is an Invalid Override (e.g., "The parameter type doesn't match the overridden method"):**
|
|
87
|
+
- Widen the parameter type to match the superclass, OR
|
|
88
|
+
- Add the `covariant` keyword to the parameter if tightening the type is intentionally required by the domain logic.
|
|
70
89
|
|
|
71
90
|
**4. Verify fixes (Feedback Loop)**
|
|
91
|
+
Run the validator. Review errors. Fix.
|
|
72
92
|
|
|
73
93
|
```bash
|
|
74
94
|
dart analyze .
|
|
75
95
|
dart test
|
|
76
96
|
```
|
|
77
97
|
|
|
98
|
+
- **If `dart analyze` reports errors:** Return to Step 3.
|
|
99
|
+
- **If `dart test` fails with a `TypeError`:** You have introduced an invalid explicit cast (`as T`) or accessed an uninitialized `late` variable. Locate the runtime failure and correct the type hierarchy or initialization order.
|
|
100
|
+
|
|
78
101
|
## Examples
|
|
79
102
|
|
|
80
|
-
### Fixing Dynamic List Assignments
|
|
103
|
+
### Example: Fixing Dynamic List Assignments
|
|
81
104
|
|
|
82
|
-
**Input (Fails):**
|
|
105
|
+
**Input (Fails Static Analysis):**
|
|
83
106
|
|
|
84
107
|
```dart
|
|
85
108
|
void printInts(List<int> a) => print(a);
|
|
@@ -87,11 +110,12 @@ void printInts(List<int> a) => print(a);
|
|
|
87
110
|
void main() {
|
|
88
111
|
final list = []; // Inferred as List<dynamic>
|
|
89
112
|
list.add(1);
|
|
90
|
-
|
|
113
|
+
list.add(2);
|
|
114
|
+
printInts(list); // Error: List<dynamic> can't be assigned to List<int>
|
|
91
115
|
}
|
|
92
116
|
```
|
|
93
117
|
|
|
94
|
-
**Output (Passes):**
|
|
118
|
+
**Output (Passes Static Analysis):**
|
|
95
119
|
|
|
96
120
|
```dart
|
|
97
121
|
void printInts(List<int> a) => print(a);
|
|
@@ -99,27 +123,62 @@ void printInts(List<int> a) => print(a);
|
|
|
99
123
|
void main() {
|
|
100
124
|
final list = <int>[]; // Explicitly typed
|
|
101
125
|
list.add(1);
|
|
126
|
+
list.add(2);
|
|
102
127
|
printInts(list);
|
|
103
128
|
}
|
|
104
129
|
```
|
|
105
130
|
|
|
106
|
-
### Fixing
|
|
131
|
+
### Example: Fixing Method Overrides (Contravariance)
|
|
132
|
+
|
|
133
|
+
**Input (Fails Static Analysis):**
|
|
134
|
+
|
|
135
|
+
```dart
|
|
136
|
+
class Animal {
|
|
137
|
+
void chase(Animal a) {}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
class Cat extends Animal {
|
|
141
|
+
@override
|
|
142
|
+
void chase(Mouse a) {} // Error: Tightening parameter type
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**Output (Passes Static Analysis):**
|
|
147
|
+
|
|
148
|
+
```dart
|
|
149
|
+
class Animal {
|
|
150
|
+
void chase(Animal a) {}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class Cat extends Animal {
|
|
154
|
+
@override
|
|
155
|
+
void chase(covariant Mouse a) {} // Explicitly marked covariant
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Example: Fixing Null Safety with `late`
|
|
107
160
|
|
|
108
|
-
**Input (Fails):**
|
|
161
|
+
**Input (Fails Static Analysis):**
|
|
109
162
|
|
|
110
163
|
```dart
|
|
111
164
|
class Thermometer {
|
|
112
|
-
String temperature; // Error: Non-nullable must be initialized
|
|
113
|
-
|
|
165
|
+
String temperature; // Error: Non-nullable instance field must be initialized
|
|
166
|
+
|
|
167
|
+
void read() {
|
|
168
|
+
temperature = '20C';
|
|
169
|
+
}
|
|
114
170
|
}
|
|
115
171
|
```
|
|
116
172
|
|
|
117
|
-
**Output (Passes):**
|
|
173
|
+
**Output (Passes Static Analysis):**
|
|
118
174
|
|
|
119
175
|
```dart
|
|
120
176
|
class Thermometer {
|
|
121
|
-
late String temperature;
|
|
122
|
-
|
|
177
|
+
late String temperature; // Defers initialization check to runtime
|
|
178
|
+
|
|
179
|
+
void read() {
|
|
180
|
+
temperature = '20C';
|
|
181
|
+
}
|
|
123
182
|
}
|
|
124
183
|
```
|
|
125
184
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: generate-test-mocks
|
|
3
3
|
description: Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
|
|
4
|
+
last_modified: Fri, 24 Apr 2026 15:13:58 GMT
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
# Testing and Mocking Dart Applications
|
|
@@ -16,10 +17,11 @@ description: Define and generate mock objects for external dependencies using `p
|
|
|
16
17
|
|
|
17
18
|
## Structuring Code for Testability
|
|
18
19
|
|
|
19
|
-
Design Dart classes to support dependency injection. Isolate complex external dependencies so they can be replaced with mock objects during testing.
|
|
20
|
+
Design Dart classes to support dependency injection. Isolate complex external dependencies (like API clients or databases) so they can be replaced with mock objects during testing.
|
|
20
21
|
|
|
21
22
|
- Inject external services (e.g., `http.Client`) through class constructors.
|
|
22
23
|
- Represent URLs strictly as `Uri` objects using `Uri.parse(string)`.
|
|
24
|
+
- Utilize Dart's object-oriented features (classes, mixins) to define clear interfaces for external interactions.
|
|
23
25
|
|
|
24
26
|
## Managing Dependencies
|
|
25
27
|
|
|
@@ -27,33 +29,38 @@ Configure the `pubspec.yaml` file with the necessary testing and code generation
|
|
|
27
29
|
|
|
28
30
|
- Add runtime dependencies (e.g., `package:http`) using `dart pub add http`.
|
|
29
31
|
- Add testing dependencies using `dart pub add dev:test dev:mockito dev:build_runner`.
|
|
30
|
-
- Import HTTP libraries with a prefix: `import 'package:http/http.dart' as http;`.
|
|
32
|
+
- Import HTTP libraries with a prefix to avoid namespace collisions: `import 'package:http/http.dart' as http;`.
|
|
31
33
|
|
|
32
34
|
## Generating Mocks
|
|
33
35
|
|
|
34
|
-
Use `package:mockito` and `build_runner` to automatically generate mock classes.
|
|
36
|
+
Use `package:mockito` and `build_runner` to automatically generate mock classes for fixed scenarios and behavior verification.
|
|
35
37
|
|
|
36
|
-
- Always use the `@GenerateNiceMocks` annotation (preferable to `@GenerateMocks`).
|
|
38
|
+
- Always use the `@GenerateNiceMocks` annotation (preferable to `@GenerateMocks` to avoid missing stub exceptions).
|
|
37
39
|
- Place the annotation in the test file, passing a list of `MockSpec<Type>()` objects.
|
|
38
40
|
- Import the generated file using the `.mocks.dart` extension.
|
|
39
41
|
- Execute `build_runner` to generate the mock files: `dart run build_runner build`.
|
|
40
42
|
|
|
41
43
|
## Implementing Unit Tests
|
|
42
44
|
|
|
43
|
-
Isolate the system under test using the generated mock objects.
|
|
45
|
+
Isolate the system under test using the generated mock objects. Use `package:test` to structure the test suite.
|
|
44
46
|
|
|
45
|
-
- **Stubbing:**
|
|
46
|
-
-
|
|
47
|
-
- **
|
|
47
|
+
- **Stubbing:** Configure mock behavior before interacting with the system under test.
|
|
48
|
+
- Use `when(mock.method()).thenReturn(value)` for synchronous methods.
|
|
49
|
+
- **CRITICAL:** Always use `thenAnswer((_) async => value)` for methods returning a `Future` or `Stream`. Never use `thenReturn` for asynchronous returns.
|
|
50
|
+
- **Verification:** Assert that the system under test interacted with the mock object correctly.
|
|
51
|
+
- Use `verify(mock.method()).called(1)` to check exact invocation counts.
|
|
52
|
+
- Use argument matchers like `any`, `anyNamed`, or `captureAny` for flexible verification.
|
|
48
53
|
|
|
49
54
|
## Workflow: Creating and Running Mocked Tests
|
|
50
55
|
|
|
56
|
+
Use the following checklist to implement and verify mocked unit tests.
|
|
57
|
+
|
|
51
58
|
### Task Progress
|
|
52
59
|
|
|
53
|
-
- [ ] 1. Identify the external dependency to mock.
|
|
60
|
+
- [ ] 1. Identify the external dependency to mock (e.g., `http.Client`).
|
|
54
61
|
- [ ] 2. Inject the dependency into the target class constructor.
|
|
55
|
-
- [ ] 3. Create a test file and add `@GenerateNiceMocks([MockSpec<Dependency>()])`.
|
|
56
|
-
- [ ] 4. Add the import directive for the generated `.mocks.dart` file.
|
|
62
|
+
- [ ] 3. Create a test file (e.g., `target_test.dart`) and add `@GenerateNiceMocks([MockSpec<Dependency>()])`.
|
|
63
|
+
- [ ] 4. Add the `part` or `import` directive for the generated `.mocks.dart` file.
|
|
57
64
|
- [ ] 5. Run `dart run build_runner build` to generate the mock classes.
|
|
58
65
|
- [ ] 6. Write the test cases using `group()` and `test()`.
|
|
59
66
|
- [ ] 7. Stub required behaviors using `when()`.
|
|
@@ -61,11 +68,23 @@ Isolate the system under test using the generated mock objects.
|
|
|
61
68
|
- [ ] 9. Verify interactions using `verify()` and assert outcomes using `expect()`.
|
|
62
69
|
- [ ] 10. Run the test suite using `dart test`.
|
|
63
70
|
|
|
71
|
+
### Feedback Loop: Test Failures
|
|
72
|
+
|
|
73
|
+
If tests fail or `build_runner` encounters errors:
|
|
74
|
+
|
|
75
|
+
1. **Run validator:** Execute `dart test` or `dart run build_runner build`.
|
|
76
|
+
2. **Review errors:** Check for missing stubs, mismatched argument matchers, or syntax errors in the generated files.
|
|
77
|
+
3. **Fix:**
|
|
78
|
+
- If a mock method throws an unexpected null error, ensure you used `@GenerateNiceMocks`.
|
|
79
|
+
- If an async stub throws an `ArgumentError`, change `thenReturn` to `thenAnswer`.
|
|
80
|
+
- If `build_runner` fails, ensure the `.mocks.dart` import matches the file name exactly.
|
|
81
|
+
4. Repeat until all tests pass.
|
|
82
|
+
|
|
64
83
|
## Examples
|
|
65
84
|
|
|
66
85
|
### High-Fidelity Mocking and Testing Example
|
|
67
86
|
|
|
68
|
-
**System Under Test (`lib/api_service.dart`)
|
|
87
|
+
**1. System Under Test (`lib/api_service.dart`)**
|
|
69
88
|
|
|
70
89
|
```dart
|
|
71
90
|
import 'dart:convert';
|
|
@@ -73,11 +92,13 @@ import 'package:http/http.dart' as http;
|
|
|
73
92
|
|
|
74
93
|
class ApiService {
|
|
75
94
|
final http.Client client;
|
|
95
|
+
|
|
76
96
|
ApiService(this.client);
|
|
77
97
|
|
|
78
98
|
Future<String> fetchData(String urlString) async {
|
|
79
99
|
final uri = Uri.parse(urlString);
|
|
80
100
|
final response = await client.get(uri);
|
|
101
|
+
|
|
81
102
|
if (response.statusCode == 200) {
|
|
82
103
|
return jsonDecode(response.body)['data'];
|
|
83
104
|
} else {
|
|
@@ -87,7 +108,7 @@ class ApiService {
|
|
|
87
108
|
}
|
|
88
109
|
```
|
|
89
110
|
|
|
90
|
-
**Test Implementation (`test/api_service_test.dart`)
|
|
111
|
+
**2. Test Implementation (`test/api_service_test.dart`)**
|
|
91
112
|
|
|
92
113
|
```dart
|
|
93
114
|
import 'package:test/test.dart';
|
|
@@ -96,6 +117,7 @@ import 'package:mockito/mockito.dart';
|
|
|
96
117
|
import 'package:http/http.dart' as http;
|
|
97
118
|
import 'package:my_app/api_service.dart';
|
|
98
119
|
|
|
120
|
+
// Generate the mock class for http.Client
|
|
99
121
|
@GenerateNiceMocks([MockSpec<http.Client>()])
|
|
100
122
|
import 'api_service_test.mocks.dart';
|
|
101
123
|
|
|
@@ -110,20 +132,28 @@ void main() {
|
|
|
110
132
|
});
|
|
111
133
|
|
|
112
134
|
test('returns data if the http call completes successfully', () async {
|
|
135
|
+
// Arrange: Stub the async HTTP GET request using thenAnswer
|
|
113
136
|
when(mockHttpClient.get(any)).thenAnswer(
|
|
114
137
|
(_) async => http.Response('{"data": "Success"}', 200),
|
|
115
138
|
);
|
|
116
139
|
|
|
140
|
+
// Act
|
|
117
141
|
final result = await apiService.fetchData('https://api.example.com/data');
|
|
142
|
+
|
|
143
|
+
// Assert
|
|
118
144
|
expect(result, 'Success');
|
|
145
|
+
|
|
146
|
+
// Verify the mock was called with the correct Uri
|
|
119
147
|
verify(mockHttpClient.get(Uri.parse('https://api.example.com/data'))).called(1);
|
|
120
148
|
});
|
|
121
149
|
|
|
122
150
|
test('throws an exception if the http call completes with an error', () {
|
|
151
|
+
// Arrange
|
|
123
152
|
when(mockHttpClient.get(any)).thenAnswer(
|
|
124
153
|
(_) async => http.Response('Not Found', 404),
|
|
125
154
|
);
|
|
126
155
|
|
|
156
|
+
// Act & Assert
|
|
127
157
|
expect(
|
|
128
158
|
apiService.fetchData('https://api.example.com/data'),
|
|
129
159
|
throwsException,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: implement-json-serialization
|
|
3
3
|
description: Create model classes with `fromJson` and `toJson` methods using `dart:convert`. Use when manually mapping JSON keys to class properties for simple data structures.
|
|
4
|
+
last_modified: Tue, 21 Apr 2026 21:44:50 GMT
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
# Serializing JSON Manually in Flutter
|
|
@@ -14,36 +15,52 @@ description: Create model classes with `fromJson` and `toJson` methods using `da
|
|
|
14
15
|
|
|
15
16
|
## Core Guidelines
|
|
16
17
|
|
|
17
|
-
- **Import `dart:convert`**:
|
|
18
|
-
- **Enforce Type Safety**:
|
|
19
|
-
- **Encapsulate Logic**: Define `fromJson` factory constructor and `toJson` method
|
|
20
|
-
- **Handle Background Parsing**: If parsing
|
|
21
|
-
- **Throw on Failure**:
|
|
18
|
+
- **Import `dart:convert`**: Utilize Flutter's built-in `dart:convert` library for manual JSON encoding (`jsonEncode`) and decoding (`jsonDecode`).
|
|
19
|
+
- **Enforce Type Safety**: Always cast the `dynamic` result of `jsonDecode()` to the expected type, typically `Map<String, dynamic>` for objects or `List<dynamic>` for arrays.
|
|
20
|
+
- **Encapsulate Serialization Logic**: Define plain model classes containing properties corresponding to the JSON structure. Implement a `fromJson` factory constructor and a `toJson` method within the model.
|
|
21
|
+
- **Handle Background Parsing**: If parsing large JSON documents (execution time > 16ms), offload the parsing logic to a separate isolate using Flutter's `compute()` function to prevent UI jank.
|
|
22
|
+
- **Throw Exceptions on Failure**: When handling HTTP responses, throw an exception if the status code is not successful (e.g., not 200 OK or 201 Created). Do not return `null`.
|
|
22
23
|
|
|
23
24
|
## Workflow: Implementing a Serializable Model
|
|
24
25
|
|
|
26
|
+
Use this checklist to implement manual JSON serialization for a data model.
|
|
27
|
+
|
|
25
28
|
**Task Progress:**
|
|
26
29
|
|
|
27
30
|
- [ ] Define the plain model class with `final` properties.
|
|
28
|
-
- [ ] Implement `factory Model.fromJson(Map<String, dynamic> json)
|
|
29
|
-
- [ ] Implement `Map<String, dynamic> toJson()
|
|
30
|
-
- [ ] Write unit tests for both methods.
|
|
31
|
+
- [ ] Implement the `factory Model.fromJson(Map<String, dynamic> json)` constructor.
|
|
32
|
+
- [ ] Implement the `Map<String, dynamic> toJson()` method.
|
|
33
|
+
- [ ] Write unit tests for both serialization methods.
|
|
31
34
|
- [ ] Run validator -> review type mismatch errors -> fix casting logic.
|
|
32
35
|
|
|
36
|
+
1. **Define the Model**: Create a class with properties matching the JSON keys.
|
|
37
|
+
2. **Implement `fromJson`**: Extract values from the `Map` and cast them to the appropriate Dart types. Use pattern matching or explicit casting.
|
|
38
|
+
3. **Implement `toJson`**: Return a `Map<String, dynamic>` mapping the class properties back to their JSON string keys.
|
|
39
|
+
4. **Validate**: Execute unit tests to ensure type safety, autocompletion, and compile-time exception handling function correctly.
|
|
40
|
+
|
|
33
41
|
## Workflow: Fetching and Parsing JSON
|
|
34
42
|
|
|
43
|
+
Use this conditional workflow when retrieving and parsing JSON from a network request.
|
|
44
|
+
|
|
35
45
|
**Task Progress:**
|
|
36
46
|
|
|
37
47
|
- [ ] Execute the HTTP request.
|
|
38
48
|
- [ ] Validate the response status code.
|
|
39
|
-
- [ ] Determine parsing strategy
|
|
40
|
-
- **Small payload**: Parse synchronously on the main thread.
|
|
41
|
-
- **Large payload**: Use `compute(parseFunction, response.body)`.
|
|
49
|
+
- [ ] Determine parsing strategy (Synchronous vs. Isolate).
|
|
42
50
|
- [ ] Decode and map the JSON to the model.
|
|
43
51
|
|
|
52
|
+
1. **Execute Request**: Use the `http` package to perform the network call.
|
|
53
|
+
2. **Validate Response**:
|
|
54
|
+
- If `response.statusCode == 200` (or 201 for POST), proceed to parsing.
|
|
55
|
+
- If the status code indicates failure, throw an `Exception`.
|
|
56
|
+
3. **Determine Parsing Strategy**:
|
|
57
|
+
- If parsing a **small payload** (e.g., a single object), parse synchronously on the main thread.
|
|
58
|
+
- If parsing a **large payload** (e.g., an array of thousands of objects), use `compute(parseFunction, response.body)` to parse in a background isolate.
|
|
59
|
+
4. **Decode and Map**: Pass the decoded JSON to your model's `fromJson` constructor.
|
|
60
|
+
|
|
44
61
|
## Examples
|
|
45
62
|
|
|
46
|
-
### Model Implementation
|
|
63
|
+
### High-Fidelity Model Implementation
|
|
47
64
|
|
|
48
65
|
```dart
|
|
49
66
|
import 'dart:convert';
|
|
@@ -53,17 +70,59 @@ class User {
|
|
|
53
70
|
final String name;
|
|
54
71
|
final String email;
|
|
55
72
|
|
|
56
|
-
const User({
|
|
73
|
+
const User({
|
|
74
|
+
required this.id,
|
|
75
|
+
required this.name,
|
|
76
|
+
required this.email,
|
|
77
|
+
});
|
|
57
78
|
|
|
79
|
+
// Factory constructor for deserialization
|
|
58
80
|
factory User.fromJson(Map<String, dynamic> json) {
|
|
59
81
|
return switch (json) {
|
|
60
|
-
{
|
|
61
|
-
|
|
82
|
+
{
|
|
83
|
+
'id': int id,
|
|
84
|
+
'name': String name,
|
|
85
|
+
'email': String email,
|
|
86
|
+
} =>
|
|
87
|
+
User(
|
|
88
|
+
id: id,
|
|
89
|
+
name: name,
|
|
90
|
+
email: email,
|
|
91
|
+
),
|
|
62
92
|
_ => throw const FormatException('Failed to load User.'),
|
|
63
93
|
};
|
|
64
94
|
}
|
|
65
95
|
|
|
66
|
-
|
|
96
|
+
// Method for serialization
|
|
97
|
+
Map<String, dynamic> toJson() {
|
|
98
|
+
return {
|
|
99
|
+
'id': id,
|
|
100
|
+
'name': name,
|
|
101
|
+
'email': email,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Synchronous Parsing (Small Payload)
|
|
108
|
+
|
|
109
|
+
```dart
|
|
110
|
+
import 'dart:convert';
|
|
111
|
+
import 'package:http/http.dart' as http;
|
|
112
|
+
|
|
113
|
+
Future<User> fetchUser(http.Client client, int userId) async {
|
|
114
|
+
final response = await client.get(
|
|
115
|
+
Uri.parse('https://api.example.com/users/$userId'),
|
|
116
|
+
headers: {'Accept': 'application/json'},
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
if (response.statusCode == 200) {
|
|
120
|
+
// Decode returns dynamic, cast to Map<String, dynamic>
|
|
121
|
+
final Map<String, dynamic> jsonMap = jsonDecode(response.body) as Map<String, dynamic>;
|
|
122
|
+
return User.fromJson(jsonMap);
|
|
123
|
+
} else {
|
|
124
|
+
throw Exception('Failed to load user');
|
|
125
|
+
}
|
|
67
126
|
}
|
|
68
127
|
```
|
|
69
128
|
|
|
@@ -74,14 +133,20 @@ import 'dart:convert';
|
|
|
74
133
|
import 'package:flutter/foundation.dart';
|
|
75
134
|
import 'package:http/http.dart' as http;
|
|
76
135
|
|
|
136
|
+
// Top-level function required for compute()
|
|
77
137
|
List<User> parseUsers(String responseBody) {
|
|
78
138
|
final parsed = (jsonDecode(responseBody) as List<dynamic>).cast<Map<String, dynamic>>();
|
|
79
139
|
return parsed.map<User>((json) => User.fromJson(json)).toList();
|
|
80
140
|
}
|
|
81
141
|
|
|
82
142
|
Future<List<User>> fetchUsers(http.Client client) async {
|
|
83
|
-
final response = await client.get(
|
|
143
|
+
final response = await client.get(
|
|
144
|
+
Uri.parse('https://api.example.com/users'),
|
|
145
|
+
headers: {'Accept': 'application/json'},
|
|
146
|
+
);
|
|
147
|
+
|
|
84
148
|
if (response.statusCode == 200) {
|
|
149
|
+
// Offload expensive parsing to a background isolate
|
|
85
150
|
return compute(parseUsers, response.body);
|
|
86
151
|
} else {
|
|
87
152
|
throw Exception('Failed to load users');
|