Link.fromJson constructor
Link.fromJson(
- dynamic data
Create a new link from data
(ie. a decoded JSON object or a href link).
data
is allowed to be Map<String, dynamic>
(containing link
attributes) or String
(containing only a href
part of a link).
Throws FormatException
if cannot parse data.
Implementation
factory Link.fromJson(dynamic data) {
try {
if (data is Map<String, dynamic>) {
return Link(
// get required fields (throws if data is unavailable or invalid)
href: Uri.parse(data['href'] as String),
// optional fields (null if data is unavailable, throws if invalid)
rel: data['rel'] as String?,
type: data['type'] as String?,
hreflang: data['hreflang'] as String?,
title: data['title'] as String?,
length: data['length'] as int?,
);
} else if (data is String) {
return Link(href: Uri.parse(data));
}
throw FormatException('Cannot create a link from $data');
} on FormatException {
rethrow;
} catch (e) {
throw FormatException('Cannot create a link', e);
}
}