You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中“No host specified in URI file:///”错误的解决求助

Flutter错误修复:Invalid argument(s): No host specified in URI file:///

我在Flutter开发中遇到错误Invalid argument(s): No host specified in URI file:///,以下是我的实现逻辑及最终修复方案。

问题场景与代码实现

获取图片URL的函数

Future<String> getProfileData() async {
    try {
      await apiBaseHelper.getAPICall(getUserData, {}).then((getData) async => {
            if (getData.containsKey('data'))
              {
                profileImg = getData["data"]["file"]["url"],
                setState(() {}),
              }
          });
    } catch (e) {
      print("something went wrong");
    }
    return profileImg;
  }

在initState中调用该函数

@override
  void initState() {
    super.initState();
    getProfileData().then((value) => {
          profileImg = value.toString(),
          print(profileImg),
        });
    _pageController = PageController(initialPage: _selectedIndex);
  }

图片使用代码

Container(
                    decoration: BoxDecoration(
                      border: Border(
                        top: BorderSide(
                          width: 3,
                          color: _selectedIndex == 3
                              ? colors.primary
                              : Colors.transparent,
                        ),
                      ),
                    ),
                    width: 50,
                    child: CircleAvatar(
                      backgroundColor: Colors.white,
                      backgroundImage: NetworkImage(profileImg),
                    ),
                  ),

变量声明

String profileImg = "";

补充getAPICall函数实现

Future<dynamic> getAPICall(Uri apiEndPoint, Map<String, Object> param) async {
    // print("In get api");
    // print(apiEndPoint);
    var responseJson;

    try {
      final finalUri = apiEndPoint.replace(queryParameters: param); //USE THIS
      SharedPreferences prefs = await SharedPreferences.getInstance();

      String? loginToken = prefs.getString(token);
      final response = await get(finalUri, headers: {
        "Authorization": 'Bearer ${loginToken}',
        "Content-Type": "application/json"
      }).timeout(const Duration(seconds: timeOut));
      // print("param****$param****$finalUri");
      // print("respon****${response.statusCode}");

      responseJson = _response(response);
      // print("responjson****$responseJson");
    } on SocketException {
      throw FetchDataException('No Internet connection');
    } on TimeoutException {
      throw FetchDataException('Something went wrong, try again later');
    }

    return responseJson;
  }

修复方案

修改CircleAvatar的backgroundImage属性,当profileImg为空字符串时将该属性设为null,避免传入空URL触发错误:

backgroundImage: profileImg == "" ? null : NetworkImage(profileImg),

内容的提问来源于stack exchange,提问作者Rudransh Singh Mahra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 05:37:52