setFromURL method
Implementation
Future<bool> setFromURL(final BuildContext context, {
required final String url,
final Map<String, String>? headers,
final int ? maxSize,
final bool ? alive,
}) async {
// Validate URL format
if (!_urlPattern.hasMatch(url)) {
error = true;
fromLoading = false;
throw Exception("The URL is not valid");
}
// print(url);
Request request = Request("GET", Uri.parse(url))..followRedirects = false;
// Add headers if provided
if (headers != null) request.headers.addAll(headers);
return await request.send().then((value) async {
if (value.statusCode == 200) {
try {
final image_name_extendion = url.split('/').last.split('.');
String name = image_name_extendion.first;
if(alive != null) {
if(!alive) name = '${DateTime.now().millisecondsSinceEpoch}${Random().nextInt(10000)}-$name';
} else {
name = '${DateTime.now().millisecondsSinceEpoch}${Random().nextInt(10000)}-$name';
}
await value.stream.toBytes().then((bytes) async => await setFromBytes(
name : name,
bytes : bytes,
extension : image_name_extendion.last,
maxSize : maxSize,
))
.onError((error, stackTrace) {
_reset(error: true);
return false;
});
return true;
} catch (e) {
_reset(error: true);
return false;
}
} else {
_reset(error: true);
return false;
}
}).onError((error, stackTrace){
_reset(error: true);
return false;
});
}