Skip to content

Commit

Permalink
Change cast in json parsing (flutter#137708)
Browse files Browse the repository at this point in the history
`jsonDecode` decodes lists as `List<Object?>`, so the cast to `List<Object>` fails at runtime in sound null safety mode.
  • Loading branch information
iinozemtsev authored Nov 6, 2023
1 parent 3649deb commit defa4bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BlockHashes {
blockSize: obj['blockSize']! as int,
totalSize: obj['totalSize']! as int,
adler32: Uint32List.view(base64.decode(obj['adler32']! as String).buffer),
md5: (obj['md5']! as List<Object>).cast<String>(),
md5: (obj['md5']! as List<Object?>).cast<String>(),
fileMd5: obj['fileMd5']! as String,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,26 @@ void main() {
expect(file.readAsStringSync(), content2);
});
});

group('BlockHashes', () {
test('json conversion works normally', () {
const String json = '''
{
"blockSize":4,
"totalSize":18,
"adler32":"7ACcAu0AoALuAKQC7wCoApQA+gA=",
"md5": [
"zB0S8R/fGt05GcI5v8AjIQ==",
"uZCZ4i/LUGFYAD+K1ZD0Wg==",
"6kbZGS8T1NJl/naWODQcNw==",
"kKh/aA2XAhR/r0HdZa3Bxg==",
"34eF7Bs/OhfoJ5+sAw0zyw=="
],
"fileMd5":"VT/gkSEdctzUEUJCxclxuQ=="
}
''';
final Map<String, Object?> decodedJson = jsonDecode(json) as Map<String, Object?>;
expect(BlockHashes.fromJson(decodedJson).toJson(), decodedJson);
});
});
}

0 comments on commit defa4bc

Please sign in to comment.