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

不羁岁月 提交于 2021-02-08 14:59:55

问题


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?


回答1:


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




回答2:


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}"




回答3:


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,
      ),
    



回答4:


This works for me

child: CircleAvatar(
      backgroundImage: _newImage
          ? FileImage(imageFile)
          : imgUrl.isNotEmpty
              ? NetworkImage(imgUrl)
              : null,),


来源:https://stackoverflow.com/questions/47957796/flutter-httpclient-invalid-arguments-no-host-specified-in-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!