0

I Have a form to get data from the user. The objective is to pass the form data as an object to the Api. The Form is as follows.

    Form(
                  key: _formKey,
                  child: Column(
                    children: [
     Seperator(),
                      TextField(
                        decoration: const InputDecoration(
                          hintText: 'Add a title',
                        ),
                      ),
                      Seperator(),
                      TextField(
                        decoration: const InputDecoration(
                          hintText: 'Add a description',
                        ),
                      ),
                      Seperator(),
    TextField(
                        decoration: const InputDecoration(
                          hintText: 'Add a Price',
                        ),
                      ),
                      Seperator(),
                      TextField(
                        decoration: const InputDecoration(
                          hintText: 'Select Unit',
                        ),
                      ),
Button(
                    isProcessing: isProcessing,
                    name: 'Create Post',
                    onPressed: () {
                      final formdata = _formKey.currentState!.save();
                      createpost(formdata); // Api Function
                    },
]

// Api Function
    createpost(AddPost formdata) async {
        try {
          final service = PostService();
          final response =
              await service.createpost(formdata);
        } catch (error) {
          setErrorMessage(error.toString());
        }

How can I pass the Form data as an object to the Api Function?

Febin Johnson
  • 277
  • 1
  • 6
  • 21

1 Answers1

1

enter image description hereYou can run the API in Postman and use autogenerated code in your flutter app. You can also convert your curl request into postman request using this link and then use auto generated code

You can also use this package to pass form data. here is an example

var dio = Dio();
 var formData = FormData.fromMap({
 'name': 'wendux',
  'age': 25,
  'file': await MultipartFile.fromFile('./text.txt',filename: 
 'upload.txt')
 });
  var response = await dio.post('/info', data: formData);
Wali Khan
  • 586
  • 1
  • 5
  • 13