Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter 添加图片到formdata出错 #13

Closed
keluokeda opened this issue May 31, 2018 · 3 comments
Closed

flutter 添加图片到formdata出错 #13

keluokeda opened this issue May 31, 2018 · 3 comments

Comments

@keluokeda
Copy link

/**
   * 更新用户信息
   */
  Future<Stream<BaseJsonBean>> updateUserInfo(
      File file, String userName, String description) {
    print("file = $file");
    FormData formData = FormData.from({
      "userName": userName,
      "description": description,
      "file": UploadFileInfo(file, "filename.jpg")
    });

    return _dioPut("/users", formData, (json) {
      return BaseJsonBean.fromJson(json);
    });
  }

  Future<Stream<T>> _dioPut<T>(
      var path, FormData formData, T fromJson(dynamic json)) async {
    return _dio
        .put(path, data: formData)
        .then((response) => response.data)
        .then(fromJson)
        .asStream();
  } 

调用方法后报错

I/flutter (16289): file = File: '/storage/emulated/0/Pictures/Screenshots/S80424-171652.jpg'
V/SettingsInterface(16289):  from settings cache , name = sound_effects_enabled , value = 1
E/flutter (16289): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (16289): DioError [DioErrorType.DEFAULT]: Converting object to an encodable object failed: Instance of 'UploadFileInfo'#0      _JsonStringifier.writeObject (dart:convert/json.dart:705:7)
E/flutter (16289): #1      _JsonStringifier.writeMap (dart:convert/json.dart:788:7)
E/flutter (16289): #2      _JsonStringifier.writeJsonValue (dart:convert/json.dart:743:21)
E/flutter (16289): #3      _JsonStringifier.writeObject (dart:convert/json.dart:695:9)
E/flutter (16289): #4      _JsonStringStringifier.printOn (dart:convert/json.dart:904:17)
E/flutter (16289): #5      _JsonStringStringifier.stringify (dart:convert/json.dart:886:5)
E/flutter (16289): #6      JsonEncoder.convert (dart:convert/json.dart:287:30)
E/flutter (16289): #7      JsonCodec.encode (dart:convert/json.dart:186:45)
E/flutter (16289): #8      DefaultTransformer.transformRequest (package:dio/src/TransFormer.dart:72:21)
E/flutter (16289): <asynchronous suspension>
E/flutter (16289): #9      Dio._transformData (package:dio/src/Dio.dart:473:32)
E/flutter (16289): <asynchronous suspension>
E/flutter (16289): #10     Dio._makeRequest (package:dio/src/Dio.dart:375:28)
E/flutter (16289): <asynchronous suspension>
E/flutter (16289): #11     Dio._request.<anonymous closure>.<anonymous closure> (package:dio/src/Dio.dart:308:20)
E/flutter (16289): #12     _RootZone.runUnary (dart:async/zone.dart:1381:54)
E/flutter (16289): #13     _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter (16289): #14     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633:45)
E/flutter (16289): #15     Future._propagateToListeners (dart:async/future_impl.dart:662:32)
E/flutter (16289): #16     Future._completeWithValue (dart:async/future_impl.dart:477:5)
E/flutter (16289): #17     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:507:7)
E/flutter (16289): #18     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (16289): #19     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (16289): 
E/flutter (16289): #0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1114:29)
E/flutter (16289): #1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (16289): #2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

是调用的方式不对吗

@keluokeda
Copy link
Author

我看了下文档,好像是上传文件只有post,put不可以吗

@wendux
Copy link
Contributor

wendux commented May 31, 2018

put 方法是不支持携带formdata 的,因为目前很多后台框架都不支持,请求方法换成post 即可。

@keluokeda
Copy link
Author

@wendux 十分感谢你的答复。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants