11import 'dart:convert' ;
2+ import 'dart:developer' as dev;
23
34import '../../domain/entities/block_document.dart' ;
45import '../../domain/entities/block_entity.dart' ;
@@ -13,15 +14,41 @@ class JsonDocumentSource implements DocumentRepository {
1314
1415 @override
1516 BlockDocument parse (String jsonString) {
16- final Map <String , dynamic > root = jsonDecode (jsonString) as Map <String , dynamic >;
17- final rawBlocks = (root['blocks' ] as List <dynamic >? ) ?? [];
17+ late final Map <String , dynamic > root;
18+ try {
19+ root = jsonDecode (jsonString) as Map <String , dynamic >;
20+ } on FormatException catch (_) {
21+ return BlockDocument (
22+ time: DateTime .now ().millisecondsSinceEpoch,
23+ version: '' ,
24+ blocks: const [],
25+ );
26+ } on TypeError catch (_) {
27+ return BlockDocument (
28+ time: DateTime .now ().millisecondsSinceEpoch,
29+ version: '' ,
30+ blocks: const [],
31+ );
32+ }
33+
34+ final rawBlocks =
35+ root['blocks' ] is List ? root['blocks' ] as List <dynamic > : < dynamic > [];
1836
1937 final blocks = rawBlocks
2038 .whereType <Map <String , dynamic >>()
2139 .map ((b) {
22- final type = (b['type' ] as String ? ) ?? '' ;
23- final data = (b['data' ] as Map <String , dynamic >? ) ?? {};
24- return registry.parse (type, data);
40+ final type = b['type' ] is String ? b['type' ] as String : '' ;
41+ final data = b['data' ] is Map <String , dynamic >
42+ ? b['data' ] as Map <String , dynamic >
43+ : < String , dynamic > {};
44+ final entity = registry.parse (type, data);
45+ if (entity == null && type.isNotEmpty) {
46+ dev.log (
47+ 'Unknown block type ignored: "$type "' ,
48+ name: 'EditorJSFlutter' ,
49+ );
50+ }
51+ return entity;
2552 })
2653 .whereType <BlockEntity >()
2754 .toList ();
0 commit comments