尝试使用Flutter将数据发送到API服务器时出错

时间:2020-06-18 19:24:19

标签: android flutter dart flutter-desktop

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

未为类型“ FormData”定义方法“ add”。 ([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;
  }

1 个答案:

答案 0 :(得分:0)

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

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