@bdayadev/flutter-ultra-mcp 1.12.0 → 1.13.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/dart/ultra_flutter/pubspec.yaml +1 -1
- package/dart/ultra_flutter_devtools/pubspec.yaml +1 -1
- package/package.json +1 -1
- 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/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/resolve-package-conflicts/SKILL.md +41 -12
- package/skills/run-static-analysis/SKILL.md +29 -16
- package/skills/setup-declarative-routing/SKILL.md +166 -30
- package/skills/setup-localization/SKILL.md +90 -20
- 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-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');
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: migrate-to-checks-package
|
|
3
3
|
description: Replace the usage of `expect` and similar functions from `package:matcher` to `package:checks` equivalents.
|
|
4
|
+
last_modified: Fri, 24 Apr 2026 15:15:22 GMT
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
# Migrating Dart Tests to Package Checks
|
|
@@ -9,11 +10,14 @@ description: Replace the usage of `expect` and similar functions from `package:m
|
|
|
9
10
|
|
|
10
11
|
- [Dependency Management](#dependency-management)
|
|
11
12
|
- [Syntax Migration Guidelines](#syntax-migration-guidelines)
|
|
13
|
+
- [Utilizing Dart MCP Tools](#utilizing-dart-mcp-tools)
|
|
12
14
|
- [Migration Workflow](#migration-workflow)
|
|
13
15
|
- [Examples](#examples)
|
|
14
16
|
|
|
15
17
|
## Dependency Management
|
|
16
18
|
|
|
19
|
+
Manage dependencies using the Dart Tooling MCP Server `pub` tool or standard CLI commands.
|
|
20
|
+
|
|
17
21
|
- Add `package:checks` as a `dev_dependency` using `dart pub add dev:checks`.
|
|
18
22
|
- Remove `package:matcher` if it is explicitly listed in the `pubspec.yaml` (note: it is often transitively included by `package:test`, which is fine).
|
|
19
23
|
- Import `package:checks/checks.dart` in all test files undergoing migration.
|
|
@@ -22,35 +26,47 @@ description: Replace the usage of `expect` and similar functions from `package:m
|
|
|
22
26
|
|
|
23
27
|
Transition test assertions from the `package:matcher` syntax to the literate API provided by `package:checks`.
|
|
24
28
|
|
|
25
|
-
- **Basic Equality:** Replace `expect(actual, equals(expected))` with `check(actual).equals(expected)`.
|
|
29
|
+
- **Basic Equality:** Replace `expect(actual, equals(expected))` or `expect(actual, expected)` with `check(actual).equals(expected)`.
|
|
26
30
|
- **Type Checking:** Replace `expect(actual, isA<Type>())` with `check(actual).isA<Type>()`.
|
|
27
31
|
- **Property Extraction:** Replace `expect(actual.property, expected)` with `check(actual).has((a) => a.property, 'property name').equals(expected)`.
|
|
28
32
|
- **Cascades for Multiple Checks:** Use Dart's cascade operator (`..`) to chain multiple expectations on a single subject.
|
|
29
33
|
- **Asynchronous Expectations:**
|
|
30
34
|
- If checking a `Future`, `await` the `check` call: `await check(someFuture).completes((r) => r.equals(expected));`.
|
|
31
|
-
- If checking a `Stream`, wrap it in a `StreamQueue` for multiple checks.
|
|
35
|
+
- If checking a `Stream`, wrap it in a `StreamQueue` for multiple checks, or use `.withQueue` for single/broadcast checks.
|
|
36
|
+
|
|
37
|
+
## Utilizing Dart MCP Tools
|
|
38
|
+
|
|
39
|
+
Leverage the Dart MCP Server tools to automate and validate the migration process.
|
|
40
|
+
|
|
41
|
+
- Use `pub` to run `dart pub get` or `dart pub add`.
|
|
42
|
+
- Use `analyze_files` to run static analysis on the project or specific paths.
|
|
43
|
+
- Use `run_tests` to execute Dart or Flutter tests with an agent-centric UX. ALWAYS use this instead of shell commands like `dart test`.
|
|
44
|
+
- Use `dart_fix` to apply automated fixes if applicable.
|
|
32
45
|
|
|
33
46
|
## Migration Workflow
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- [ ]
|
|
38
|
-
- [ ]
|
|
39
|
-
- [ ]
|
|
40
|
-
- [ ]
|
|
48
|
+
Copy and use the following checklist to track progress when migrating a test suite:
|
|
49
|
+
|
|
50
|
+
- [ ] **Task Progress**
|
|
51
|
+
- [ ] Add `package:checks` as a dev dependency.
|
|
52
|
+
- [ ] Identify all test files using `package:matcher` (`expect` calls).
|
|
53
|
+
- [ ] Import `package:checks/checks.dart` in target test files.
|
|
54
|
+
- [ ] Rewrite all `expect(...)` statements to `check(...)` statements.
|
|
55
|
+
- [ ] Run static analyzer (`analyze_files`).
|
|
56
|
+
- [ ] Run tests (`run_tests`).
|
|
41
57
|
|
|
42
58
|
### Feedback Loop: Static Analysis
|
|
43
59
|
|
|
44
|
-
1. Run the
|
|
45
|
-
2. Review any static analysis warnings or errors.
|
|
60
|
+
1. Run the `analyze_files` tool on the modified test directories.
|
|
61
|
+
2. Review any static analysis warnings or errors (e.g., missing imports, incorrect generic types on `isA`, unawaited futures).
|
|
46
62
|
3. Fix the warnings.
|
|
47
|
-
4. Repeat until zero issues.
|
|
63
|
+
4. Repeat until the analyzer returns zero issues.
|
|
48
64
|
|
|
49
65
|
### Feedback Loop: Test Validation
|
|
50
66
|
|
|
51
|
-
1. Run the
|
|
52
|
-
2. If tests fail, review the failure output. `package:checks` provides detailed context.
|
|
53
|
-
3. Adjust the `check()` expectations or the underlying code.
|
|
67
|
+
1. Run the `run_tests` tool.
|
|
68
|
+
2. If tests fail, review the failure output. `package:checks` provides detailed context (e.g., `Which: has length of <2>`).
|
|
69
|
+
3. Adjust the `check()` expectations or the underlying code to resolve the failure.
|
|
54
70
|
4. Repeat until all tests pass.
|
|
55
71
|
|
|
56
72
|
## Examples
|
|
@@ -110,6 +126,22 @@ await check(Future.value(10)).completes((it) => it.equals(10));
|
|
|
110
126
|
await check(Future.error('oh no')).throws<String>().equals('oh no');
|
|
111
127
|
```
|
|
112
128
|
|
|
129
|
+
### Asynchronous Streams
|
|
130
|
+
|
|
131
|
+
**Input (`matcher`):**
|
|
132
|
+
|
|
133
|
+
```dart
|
|
134
|
+
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
|
|
135
|
+
await expectLater(stdout, emitsThrough('Ready'));
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**Output (`checks`):**
|
|
139
|
+
|
|
140
|
+
```dart
|
|
141
|
+
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
|
|
142
|
+
await check(stdout).emitsThrough((it) => it.equals('Ready'));
|
|
143
|
+
```
|
|
144
|
+
|
|
113
145
|
## Flutter Ultra Integration
|
|
114
146
|
|
|
115
147
|
Validate the migration with static analysis and tests:
|