toUriData static method
Implementation
static Future<UriData?> toUriData(String url, {String? domain}) async {
try {
// parse the url
Uri? uri = parse(url, domain: domain);
// failed parse
if (uri == null) return null;
// already a data uri
if (uri.data != null) return uri.data;
// file reference
if (uri.scheme == "file") {
var filepath = uri.asFilePath();
if (filepath == null) return null;
var file = File(filepath);
var bytes = await file.readAsBytes();
var mime = await Mime.type(uri.url);
return UriData.fromBytes(bytes, mimeType: mime);
}
// file reference
if (uri.scheme == "assets") {
var assetpath = "${uri.scheme}/${uri.host}${uri.path}";
ByteData bytes = await rootBundle.load(assetpath);
var mime = await Mime.type(uri.url);
return UriData.fromBytes(bytes.buffer.asUint8List(), mimeType: mime);
}
// remote image file
var response = await Http.get(uri.url);
if (response.statusCode == HttpStatus.ok) {
var bytes = response.bytes;
var mime = await Mime.type(uri.url);
return UriData.fromBytes(bytes, mimeType: mime);
}
} catch (e) {
Log().info("Error in toUriData using $url. Error is $e");
}
return null;
}