I have a PHP backend and I use it on my localhost so everything okay but I have a problem that the image URL I get from API is a wrong path and I can't change it from server-side so I decide to fix it on my client-side
I can display an image on my emulator with this path :
http://10.0.2.2:8000/storage/app/public/171/conversions/api-icon.jpg
and the API gives me this path
http://192.168.1.114/multi-restaurants/public/storage/app/public/171/conversions/api-icon.jpg
I fix it by making a function to change the path but it takes a lot of work like I should put this function in every place I wanna display an image!!
I'm sure there is a way to change the path directly from the model when I receive the api here is my model
class Media {
String id;
String name;
String url;
String thumb;
String icon;
String size;
Media();
Media.fromJSON(Map<String, dynamic> jsonMap)
: id = jsonMap['id'].toString(),
name = jsonMap['name'],
url = jsonMap["url"] ,
thumb = jsonMap['thumb'],
icon = jsonMap['icon'],
size = jsonMap['formated_size'];
This function that I'm using in every class to change path Url
String changepath(String uuu) {
final uri = Uri.parse(uuu);
print("This is $uri");
if (uri.path.contains("multi-restaurants")) {
print("http://10.0.2.2:8000/${uri.pathSegments[2]}/${uri.pathSegments[3]}/${uri.pathSegments[4]}/${uri.pathSegments[5]}/${uri.pathSegments[6]}");
return"http://10.0.2.2:8000/${uri.pathSegments[2]}/${uri.pathSegments[3]}/${uri.pathSegments[4]}/${uri.pathSegments[5]}/${uri.pathSegments[6]}";
}
}
}
If the API constantly sending the URL in "http://192.168.1.114/multi-r ... " in this format, then there is a solution for it, don't know is this a smart move or anything you looking for.Just create String type Function,pass the url from jsonurl = changer(jsonMap["url"]);
and use it to convert the URL to desired URL.
String changer(String _string1) {
String _string2 = _string1.replaceAll("http://192.168.1.114/multi-restaurants/public", "http://10.0.2.2:8000");
return _string2;
}
I Solve the Problem with Create a Helper Class and put inside it a static function and call it in every widget I wanna display (Url)
static String changer(String _string1) {
String _string2 = _string1.replaceAll(
"http://192.168.1.114/multi-restaurants/public",
"http://10.0.2.2:8000");
print(_string2);
return _string2;
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.