'How to make post request as form-data with Get x package in flutter?

Hi i need to send signup body as form-data in flutter with getx.

here is my model clas

class SignupBody {
int? responseStatus;
String? message;
String? userId;
String? name;
String? email;
String? role;
String? country;
File? photo;
String? university;
List<String>? qualification;
List<String>? interests;

SignupBody(
  {this.responseStatus,
  this.message,
  this.userId,
  this.name,
  this.email,
  this.role,
  this.country,
  this.photo,
  this.university,
  this.qualification,
  this.interests});  
}

i need to post this body as form-data how can i post as form-data ?



Solution 1:[1]

You can use dio package to send formData.

  • First make formdata

    var formData = await formDataMap(
      checkbox: checkbox,
      taskDetail: _task,
      pageNumber: _currentQuestion.value,
      valueString: value,
      valueType: valueType,
      formFieldInstanceID:
      task.formFields[_currentQuestion.value].formFieldInstanceId,
    );
    
  • Then add it in data parameter of dio_client Example

     _dio.putUri(
     Uri.parse(uri),
     data: data,
    

    );

Solution 2:[2]

You can use FormData(map) from get package.

FormData formdata = FormData(map);
post(url, formdata);

Solution 3:[3]

Use the dart HTTP package and convert your class SignupBody into JSON:

import 'package:http/http.dart' as http;

var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');

print(await http.read(Uri.parse('https://example.com/foobar.txt')));

Source:

https://pub.dev/packages/http

(If that is the best choice, depends on your setup which you did not describe in your question. If you use Firebase use FlutterFire.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aashish Jha
Solution 2 InFicial Infotech
Solution 3