fromJson static method
Lê o alvo do JSON. Lança FormatoDaRotinaException se o tipo não existe (uma
versão nova do app) ou se falta um campo. onde diz de que item é, para a
mensagem.
Implementation
static RotinaAlvo fromJson(
Map<String, Object?> json, {
required String onde,
}) {
final tipo = textoDoJson(json, 'tipo', onde: onde);
return switch (tipo) {
'trilha' => AlvoDeTrilha(
trilha: _trilhaDoJson(json, onde: onde),
passo: textoDoJson(json, 'passo', onde: onde),
),
'musica' || 'cifra' => AlvoDePartitura(
trilha: TrilhaDaPartitura.values.byName(tipo),
referencia: textoDoJson(json, 'referencia', onde: onde),
chave: textoDoJson(json, 'chave', onde: onde),
),
'bloco' => AlvoDeBloco(
texto: textoDoJson(json, 'texto', onde: onde),
midias: [
for (final midia in listaDoJson(
json['midias'] ?? const [],
onde: onde,
))
MidiaDoBloco.fromJson(mapaDoJson(midia, onde: '$onde, mídia')),
],
),
_ => throw FormatoDaRotinaException(
'$onde: tipo de alvo desconhecido (`$tipo`). Atualize o app.',
),
};
}