【问题标题】:Error when trying to use Flutter to send data to an API server尝试使用 Flutter 向 API 服务器发送数据时出错
【发布时间】:2020-10-08 23:26:00
【问题描述】:

使用 Flutter 向 API Server 发送 POST 请求时出现以下错误。

没有为“FormData”类型定义“add”方法。 (undefined_method at [next_hour] lib\ui\edit_profile.dart:122)

    FormData formdata = new FormData();

    Future <String> updateProfile() async{
    newDob  = DateFormat("y-MM-dd").format(_dateTime);
    newMobile=_editMobileController.text;
    print("dek: $newMobile");
    newName=_editNameController.text;
    String imagefileName = tmpFile != null ? tmpFile.path.split('/').last: '';
    try{
      if(imagefileName != ''){
        formdata.add(
            "image", new UploadFileInfo(tmpFile, imagefileName)
        );
      }
      formdata.add(
          "email", sEmail
      );
      formdata.add(
          "current_password", sPass
      );
      formdata.add(
          "new_password", sPass
      );
      formdata.add(
          "dob", newDob
      );
      formdata.add(
          "mobile", newMobile
      );
      formdata.add(
          "name", newName
      );

      await dio.post(APIData.userProfileUpdate, data: formdata, options: Options(
          method: 'POST',

          headers: {
            // ignore: deprecated_member_use
            HttpHeaders.AUTHORIZATION: fullData,
          }
      )).then(
              (response) {
            setState(() {
              isShowIndicator = false;
            });
            _profileUpdated(context);
          }
      )
          .catchError((error) => print(error.toString()));

    }
    catch(e){
      setState(() {
        isShowIndicator = false;
      });
      print(e);
    }
    return null;
  }

【问题讨论】:

  • 我已经编辑了你的问题来解释你想要做什么。
  • 你找到解决办法了吗

标签: android flutter dart flutter-desktop


【解决方案1】:

看起来 add 在最新版本的 dio 中不再可用,您可以执行以下操作:

FormData.fromMap(<String, dynamic>{ 
"image": new UploadFileInfo(tmpFile, imagefileName),
"mobile" : newMobile, }); 
... 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2018-07-21
    相关资源
    最近更新 更多