Post 出现 flutter 异常无效参数 URI 中未指定主机

Post with flutter exception invalid argument no host specified in URI

我一直在尝试从这段代码中 post 我的数据,但它一直告诉我没有指定主机的 URI。堆栈溢出中的另一种情况一直告诉使用http或https,但我已经从一开始就使用它并且它不起作用。有人能告诉我有没有逻辑错误之类的。 PS: GroupCode 需要作为列表['']返回。

  String sourceDocType = 'STATUS_UPDATE';
  List listTampungan = [];

  void initState() {
    super.initState();
    news = TextEditingController();
    getData();
  }

  Future<List> getData() async {
    prefs = await SharedPreferences.getInstance();
    name = prefs.getString('employeeName');
    id = prefs.getString('sessionId');
    location = prefs.getString('location');
    groupCode = prefs.getString('groupCode');
    var tampungan = [id, name, location, groupCode];
    return tampungan;
  }

  Future<http.Response> postNewsFeed() async {
    var url = "http:xxxx?";
    var response = await http.post(url,
        body: json.encode({
          "Type": type,
          "strEmpNo": id,
          "strNews": news,
          "strPrivacyType": privacyType,
          "strCreatedBy": id,
          "strGroupCode": ['$groupCode'],
          "strSourceDocType": sourceDocType,
          "strEncodedImage": null,
        }));
    return response;
  }

这是我调用方法的按钮:

        ElevatedButton(
            onPressed: () {
              postNewsFeed();
            },
            child: Text('SAVE')),

参考 http 包的 documentation,所有方法都已更改为现在采用 Uri url 类型的参数而不是 String url

简单url

Uriclass有特定的格式,正常的String url需要用Uri.parse解析。进行以下修改:

var url = Uri.parse("http://111.11.11.11:111/Services/SocMed/SocMedSvc.ashx");

Url 带参数

如果您还想传递查询参数,请根据您的 url.

使用一种静态方法 .http()/.https()
var _authority = '111.11.11.11:111';
var _path = '/Services/SocMed/SocMedSvc.ashx';
var _params = {};
var url = Uri.http(_authority, _path, _params);