question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Dart-dio-next] nullable fields sent when using multipart forms

See original GitHub issue

This is what is generated today with last version:

Future<Response<User>> saveProfile({ 
    String? email,
    String? firstName,
    String? lang,
    String? lastName,
    String? mobile,
    String? password,
    MultipartFile? avatar,
    CancelToken? cancelToken,
    Map<String, dynamic>? headers,
    Map<String, dynamic>? extra,
    ValidateStatus? validateStatus,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) 
....

_bodyData = FormData.fromMap(<String, dynamic>{
        r'email': encodeFormParameter(_serializers, email, const FullType(String)),
        r'firstName': encodeFormParameter(_serializers, firstName, const FullType(String)),
        r'lang': encodeFormParameter(_serializers, lang, const FullType(String)),
        r'lastName': encodeFormParameter(_serializers, lastName, const FullType(String)),
        r'mobile': encodeFormParameter(_serializers, mobile, const FullType(String)),
        r'password': encodeFormParameter(_serializers, password, const FullType(String)),
        r'avatar': avatar,
      });

Problem with that is that null fields are still sent to the payload, and even worse encodeFormParameter is forcing null field to be empty strings.

Basically here I don’t want an empty string sent as a new password ^^, so I’m getting 400 all the time because password is not correct.

I guess an easy fix would be to wrap then into a if like this:

_bodyData = FormData.fromMap(<String, dynamic>{
        if(email != null)  r'email': encodeFormParameter(_serializers, email, const FullType(String)),
        if(firstName != null)  r'firstName': encodeFormParameter(_serializers, firstName, const FullType(String)),
        if(lang != null) r'lang': encodeFormParameter(_serializers, lang, const FullType(String)),
        if(lastName != null) r'lastName': encodeFormParameter(_serializers, lastName, const FullType(String)),
        if(mobile != null) r'mobile': encodeFormParameter(_serializers, mobile, const FullType(String)),
        if(password != null) r'password': encodeFormParameter(_serializers, password, const FullType(String)),
        if(avatar != null) r'avatar': avatar,
      });

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
kuhnroyalcommented, Sep 2, 2021

Yea, already did that locally.

1reaction
jaumardcommented, Sep 1, 2021

Yes wrapping if should be based on those two flags for sure. I saying it’s “easy” because we already have all the info in the template to make it work, so no heavy development in java or something ^^

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving multipart form-data json parameter null
I receive in the controller both the file and the integer fine, but the json has all the fields as null. What could...
Read more >
Configuring a resource to receive multipart/form-data parts ...
This task provides instructions for configuring a JAX-RS method to use and produce the multipart/form-data parts. The following example illustrates an HTML ...
Read more >
Multipart request receives null content - MuleSoft Help Center
I am sending a multipart request as below to an email api- but the email api receives null content and throws and error...
Read more >
How can i read multipart request parameters lazly?
String str = null; ... String returnParamValue = null; ... Why would you use a multipart form if you aren't uploading a file,...
Read more >
Nullable Value Types - Visual Basic - Microsoft Learn
Sometimes you work with a value type that does not have a defined value in certain circumstances. For example, a field in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found