decodeMap method Null safety
- dynamic src
Decode a map
Implementation
Map<String, dynamic> decodeMap(dynamic src) {
var p = src is GsonParsable
? src
: src is String
? GsonParsable(src)
: throw ('The src is not a valid input to decode an Array from');
var map = <String, dynamic>{};
var foundComma = true;
if (p.next() != '{') {
throw ('Array has to start with a [');
}
while (p.actual() != '}') {
if (!foundComma) {
throw p.error('Expected "}" or ","');
}
foundComma = false;
_skipIgnored(p);
var key = '';
if (p.actual() == '"' || p.actual() == "'") {
key = decodeString(src);
} else {
while (_KEY_CHARACTERS.hasMatch(p.actual())) {
key += p.next();
}
}
_skipIgnored(p);
if (p.actual() != ':') {
throw p.error('Expected ":"');
}
p.skip();
_skipIgnored(p);
if (RegExp(r'''[\\[\\{\\\"\\\'0-9]''').hasMatch(p.actual()) ||
_PURE_STRING.hasMatch(p.actual())) {
map[key] = decode(p);
} else {
throw p.error('Expected "[", "\\"","\\\'", "{" or a number');
}
_skipIgnored(p);
if (p.actual() == ',') {
foundComma = true;
p.skip();
}
_skipIgnored(p);
}
if (!p.ended) p.skip();
return map;
}