Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Flutter) HTTPClient Invalid argument(s): No host specified in URI

Currently working on a little app that allows users to view a database stored on Heroku, however I am running into the aforementioned issue when using the database's URL: ".herokuapp.com/api/".

var client = createHttpClient();
var response = await client.read('<example>.herokuapp.com/api/<data>');
List data = JSON.decode(response);

Heroku doesn't seem to use HTTP(S) nor www, the latter of which I believe to be the issue.

Does anyone know how I can bypass or resolve this issue?

like image 264
Oscar Cooke-Abbott Avatar asked Dec 24 '17 03:12

Oscar Cooke-Abbott


5 Answers

This is because the app tries to get renderUrl asynchronously. Before the render Url is fetched back, it is null. NetworkImage(renderUrl ?? '') then tries to use empty string as the url and result in the exception. I am not sure if this is the best solution, but this seems to work:

There are many ways to solve this, here is some of them

  1. Add https:// before your uri

  2. For Image : I decided to check if the image url was empty before putting it in the widget:

     CircleAvatar(
        backgroundColor: Colors.grey,
        backgroundImage: _urlImage.isNotEmpty
            ? NetworkImage(_urlImagem)
            : null,
      ),
    
like image 72
Paresh Mangukiya Avatar answered Sep 19 '22 11:09

Paresh Mangukiya


I know this is an old problem but I just stumbled into this problem and fixed it by adding an "http://" before the URL.

like image 40
Rodrigo Ortega Avatar answered Oct 20 '22 09:10

Rodrigo Ortega


One of the problem is not having "http://" before your API URL;

SOLUTION Add "http://" before your API URL example API_URL = "api.openweathermap.org/data/2.5/weather?q={city%20name}&appid={your%20api%20key}"

By adding "http://" it becomes "http://api.openweathermap.org/data/2.5/weather?q={city%20name}&appid={your%20api%20key}"

like image 12
eclat_soft Avatar answered Oct 20 '22 07:10

eclat_soft


If you still get this issue in latest versions of flutter, try to check the following things.

Previously uri was in String type. Now its in Uri type for the http requests.

If we try to use the url as

String urlPath = '<example>.herokuapp.com/api/<data>';
client.read(Uri(path: urlPath));

We will get the exception as No host specified in URI

This can be solved by using parse method in Uri class

client.read(Uri.parse(urlPath));

It solves the issue.

like image 6
Sankar Arumugam Avatar answered Oct 20 '22 09:10

Sankar Arumugam


This works for me

child: CircleAvatar(
      backgroundImage: imageFile != null
          ? FileImage(imageFile)
          : imgUrl.isNotEmpty
              ? NetworkImage(imgUrl)
              : null,),
like image 2
Hamdam Muqimov Avatar answered Oct 20 '22 07:10

Hamdam Muqimov