Skip to content

Commit 5054d78

Browse files
committed
strong mode fixes
1 parent c94cb89 commit 5054d78

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

.analysis_options

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
analyzer:
2+
# Not enabling this yet – need a fix in Mockito
3+
# strong-mode: true

example/example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Item {
4242

4343
Item();
4444

45-
factory Item.fromJson(Map<String, Object> json) => _$ItemFromJson(json);
45+
factory Item.fromJson(json) => _$ItemFromJson(json);
4646
}
4747

4848
@JsonLiteral('data.json')

example/example.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/generators/json_serializable_generator.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class JsonSerializableGenerator
3434

3535
// Get all of the fields that need to be assigned
3636
// TODO: support overriding the field set with an annotation option
37-
var fields =
38-
classElement.fields.fold(<String, FieldElement>{}, (map, field) {
37+
var fields = classElement.fields.fold(<String, FieldElement>{},
38+
(Map<String, FieldElement> map, field) {
3939
map[field.name] = field;
4040
return map;
41-
}) as Map<String, FieldElement>;
41+
});
4242

4343
// Get the constructor to use for the factory
4444

@@ -231,7 +231,7 @@ String _writeAccessToVar(String varExpression, DartType searchType,
231231

232232
var itemVal = "v$depth";
233233

234-
var output = "$varExpression?.map(($itemVal) =>"
234+
var output = "($varExpression as List)?.map(($itemVal) =>"
235235
"${_writeAccessToVar(itemVal, iterableGenericType, depth: depth+1)}"
236236
")";
237237

@@ -268,7 +268,8 @@ DartType _getIterableGenericType(InterfaceTypeImpl type) {
268268

269269
bool _implementsDartList(DartType type) => _typeTest(type, _isDartList) != null;
270270

271-
DartType _typeTest(DartType type, bool tester(DartType)) {
271+
ParameterizedType _typeTest(
272+
ParameterizedType type, bool tester(ParameterizedType type)) {
272273
if (tester(type)) return type;
273274

274275
if (type is InterfaceType) {

lib/src/annotation.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ dynamic instantiateAnnotation(ElementAnnotationImpl annotation) {
3030
var element = annotation.element;
3131

3232
if (element is PropertyAccessorElementImpl) {
33-
var initializer = element.variable.computeNode().initializer
34-
as InstanceCreationExpression;
33+
var initializer = ((element as PropertyAccessorElementImpl)
34+
.variable
35+
.computeNode() as VariableDeclaration)
36+
.initializer as InstanceCreationExpression;
3537
element = initializer.staticElement;
3638
}
3739

@@ -145,7 +147,7 @@ dynamic _createFromConstructor(
145147
// field assigned in the object. Then we can take the field value and
146148
// set it as the argument value
147149
var initializer = ctor.constantInitializers.singleWhere((ci) {
148-
var expression = ci.expression;
150+
var expression = (ci as ConstructorFieldInitializer).expression;
149151
if (expression is SimpleIdentifier) {
150152
return expression.name == paramName;
151153
}

test/json_serializable_integration_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ void main() {
8888
});
8989
}
9090

91-
void _roundTripObject(object, factory(json)) {
91+
void _roundTripObject(object, factory(Map<String, dynamic> json)) {
9292
var json = _loudEncode(object);
9393

94-
var person2 = factory(JSON.decode(json));
94+
var person2 = factory(JSON.decode(json) as Map<String, dynamic>);
9595

9696
expect(person2, equals(object));
9797

test/test_files/json_test_example.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Person extends Object with _$PersonSerializerMixin {
1818

1919
Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
2020

21-
factory Person.fromJson(json) => _$PersonFromJson(json);
21+
factory Person.fromJson(Map json) => _$PersonFromJson(json);
2222

2323
bool operator ==(other) =>
2424
other is Person &&
@@ -34,7 +34,7 @@ class Order extends Object with _$OrderSerializerMixin {
3434
bool isRushed;
3535
final UnmodifiableListView<Item> items;
3636

37-
int get price => items.fold(0, (item, total) => item.price + total);
37+
int get price => items.fold(0, (total, item) => item.price + total);
3838

3939
Order([Iterable<Item> items])
4040
: this.items = new UnmodifiableListView<Item>(
@@ -57,7 +57,7 @@ class Item extends Object with _$ItemSerializerMixin {
5757

5858
Item([this.price]);
5959

60-
factory Item.fromJson(Map<String, Object> json) => _$ItemFromJson(json);
60+
factory Item.fromJson(json) => _$ItemFromJson(json);
6161

6262
bool operator ==(other) =>
6363
other is Item &&

test/test_files/json_test_example.g.dart

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)