@@ -12,7 +12,7 @@ class RestClient extends FlutterFeathersjs {
1212 Dio dio;
1313 Utils utils;
1414
15- //Using singleton to ensure we the same instance of it
15+ //Using singleton to ensure we use the same instance of it
1616 static final RestClient _restClient = RestClient ._internal ();
1717 factory RestClient () {
1818 return _restClient;
@@ -200,27 +200,71 @@ class RestClient extends FlutterFeathersjs {
200200 /// POST /serviceName
201201 /// Create a new resource with data.
202202 Future <Response <dynamic >> create (
203- {String serviceName, Map <String , dynamic > data}) async {
204- var response;
205- try {
206- response = response = await this .dio.post ("/$serviceName " , data: data);
207- } catch (e) {
208- print ("Error in rest::create" );
209- print (e);
203+ {String serviceName,
204+ Map <String , dynamic > data,
205+ containsFile = false ,
206+ hasSingleFile = true ,
207+ fileFieldName = "file" ,
208+ List <Map <String , String >> files}) async {
209+ Response <dynamic > response;
210+
211+ if (! containsFile) {
212+ try {
213+ response = await this .dio.post ("/$serviceName " , data: data);
214+ } catch (e) {
215+ print ("Error in rest::create" );
216+ print (e);
217+ }
218+ } else {
219+ FormData formData = await this .makeFormData (
220+ hasSingleFile: hasSingleFile,
221+ nonFilesFieldsMap: data,
222+ fileFieldName: fileFieldName,
223+ files: files);
224+ try {
225+ response = await this .dio.post ("/$serviceName " , data: formData);
226+ } catch (e) {
227+ print ("Error in rest::create::with:file" );
228+ print (e);
229+ }
210230 }
211231 return response;
212232 }
213233
214234 /// PUT /serviceName/_id
215235 /// Completely replace a single resource.
216236 Future <Response <dynamic >> update (
217- {String serviceName, objectId, Map <String , dynamic > data}) async {
218- var response;
219- try {
220- response = await this .dio.put ("/$serviceName " + "/$objectId " , data: data);
221- } catch (e) {
222- print ("Error in rest::update" );
223- print (e);
237+ {String serviceName,
238+ objectId,
239+ Map <String , dynamic > data,
240+ containsFile = false ,
241+ hasSingleFile = true ,
242+ fileFieldName = "file" ,
243+ List <Map <String , String >> files}) async {
244+ Response <dynamic > response;
245+
246+ if (! containsFile) {
247+ try {
248+ response =
249+ await this .dio.put ("/$serviceName " + "/$objectId " , data: data);
250+ } catch (e) {
251+ print ("Error in rest::update" );
252+ print (e);
253+ }
254+ } else {
255+ FormData formData = await this .makeFormData (
256+ hasSingleFile: hasSingleFile,
257+ nonFilesFieldsMap: data,
258+ fileFieldName: fileFieldName,
259+ files: files);
260+ try {
261+ response = await this
262+ .dio
263+ .patch ("/$serviceName " + "/$objectId " , data: formData);
264+ } catch (e) {
265+ print ("Error in rest::update::with:file" );
266+ print (e);
267+ }
224268 }
225269 return response;
226270 }
@@ -229,15 +273,39 @@ class RestClient extends FlutterFeathersjs {
229273 /// Merge the existing data of a single resources with the new data.
230274 /// NOT TESTED
231275 Future <Response <dynamic >> patch (
232- {String serviceName, objectId, Map <String , dynamic > data}) async {
233- var response;
234- try {
235- response =
236- await this .dio.patch ("/$serviceName " + "/$objectId " , data: data);
237- } catch (e) {
238- print ("Error in rest::patch" );
239- print (e);
276+ {String serviceName,
277+ objectId,
278+ Map <String , dynamic > data,
279+ containsFile = false ,
280+ hasSingleFile = true ,
281+ fileFieldName = "file" ,
282+ List <Map <String , String >> files}) async {
283+ Response <dynamic > response;
284+
285+ if (! containsFile) {
286+ try {
287+ response =
288+ await this .dio.patch ("/$serviceName " + "/$objectId " , data: data);
289+ } catch (e) {
290+ print ("Error in rest::patch" );
291+ print (e);
292+ }
293+ } else {
294+ FormData formData = await this .makeFormData (
295+ hasSingleFile: hasSingleFile,
296+ nonFilesFieldsMap: data,
297+ fileFieldName: fileFieldName,
298+ files: files);
299+ try {
300+ response = await this
301+ .dio
302+ .patch ("/$serviceName " + "/$objectId " , data: formData);
303+ } catch (e) {
304+ print ("Error in rest::patch::with:file" );
305+ print (e);
306+ }
240307 }
308+
241309 return response;
242310 }
243311
@@ -255,4 +323,45 @@ class RestClient extends FlutterFeathersjs {
255323 }
256324 return response;
257325 }
326+
327+ ///@var fieldsMap: other field non file
328+ ///@var hasSingleFile: true for signle file , false otherwise
329+ ///@ fileFieldName: the file | files field which must be send to the server
330+ ///@var files: a List map of {"filePath": the file path, "fileName": the file ame}
331+ ///if hasSingleFile is true, just the file first entry of the list otherwise looping through the
332+ ///list
333+ Future <FormData > makeFormData (
334+ {Map <String , dynamic > nonFilesFieldsMap,
335+ hasSingleFile = true ,
336+ fileFieldName = "file" ,
337+ List <Map <String , String >> files}) async {
338+ var formData = FormData ();
339+
340+ // Non file
341+ if (nonFilesFieldsMap != null ) {
342+ nonFilesFieldsMap.forEach ((key, value) {
343+ formData.fields..add (MapEntry (key, value));
344+ });
345+ }
346+
347+ if (hasSingleFile) {
348+ //File
349+ var fileData = files[0 ];
350+ formData.files.add (MapEntry (
351+ fileFieldName,
352+ await MultipartFile .fromFile (fileData["filePath" ],
353+ filename: fileData["fileName" ]),
354+ ));
355+ } else {
356+ // Non file
357+ for (var fileData in files) {
358+ formData.files.add (MapEntry (
359+ fileFieldName,
360+ await MultipartFile .fromFile (fileData["filePath" ],
361+ filename: fileData["fileName" ]),
362+ ));
363+ }
364+ }
365+ return formData;
366+ }
258367}
0 commit comments