1

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

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

    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;
  }
4

1 回答 1

0

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

FormData.fromMap(<String, dynamic>{ 
"image": new UploadFileInfo(tmpFile, imagefileName),
"mobile" : newMobile, }); 
... 
于 2021-01-12T21:05:23.737 回答