I'm having a hard time distinguishing between network image and file image.
putImage(image){
if(image.runtimeType == String){
// if(image.contains('http')){
imageInProfile = NetworkImage(image);
return imageInProfile;
} else{
imageInProfile = FileImage(File(image));
return imageInProfile;
}
}
I used 'image.runtimeType == String', however the network image and file image are both string type. Then I tried contains method because most of the network image has 'http' or 'https', but it didn't work and cause error that type '_File' is not a subtype of type 'String'. How can I solve this problem?
CodePudding user response:
To check Valid URL string you just have to use Uri.parse() like below.
bool _validURL = Uri.parse(_adVertData.webLink).isAbsolute;
CodePudding user response:
I believe you will be receiving String URL of it is network image for the other it will be a File.
If yes, you can check file type like,
if(image is String)
This will check type of image object. if it is String, you should check for the url is http url,
Uri.parse(_adVertData.webLink).isAbsolute
This will return true if it is Http url.
Please let me know if this doesn't works for you
Update:
Widget getImageBasedonType(String image){
if(image.contains("http://") ||image.contains("https://")){
return Image.network(image);
}else{
return Image.file(File(image));
}
}
here, I consider image will be network image or file path not the asset.