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] Error on nullable file

See original GitHub issue

This definition:

/api/v1/user:
    post:
      operationId: saveProfile
      description: ""
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              "$ref": "#/components/schemas/UserUpdate"
      responses:
        "200":
          description: All good
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/User"
      tags:
        - user
UserUpdate:
      type: object
      required:
        - id
        - email
        - lang
      properties:
        id:
          type: integer
        email:
          type: string
          format: email
        firstname:
          type: string
          nullable: true
        lang:
          type: string
        lastname:
          type: string
          nullable: true
        mobile:
          type: string
          nullable: true
        password:
          type: string
          nullable: true
        avatar:
          type: string
          format: binary
          nullable: true

Gives this generation:

_bodyData = FormData.fromMap(<String, dynamic>{
        r'id': encodeFormParameter(_serializers, id, const FullType(int)),
        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': MultipartFile.fromBytes(avatar, filename: r'avatar'),
      });

So it doesn’t compile because avatar (and other fields too but look null only avatar doesn’t compile) can be null. A correct generation would be:

_bodyData = FormData.fromMap(<String, dynamic>{
        r'id': encodeFormParameter(_serializers, id, const FullType(int)),
        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)),
        if (avatar != null)
          r'avatar': MultipartFile.fromBytes(avatar, filename: r'avatar'),
      });

I have tested with openapi jar 5.1.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jaumardcommented, Jul 26, 2021

Yeah I guess I will have to change my shitty API… Because it like /api/v1/dashboard/room/null currently 😕 Thanks for the inputs!

0reactions
kuhnroyalcommented, Jul 26, 2021

@jaumard Any new input here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flutter| Dio : Invalid argument(s) (value): Must not be null
myPrefs doesn't seem to be initialized !!! Try: Future login() async { Dio dio = new Dio(); var myPrefs = await SharedPreferences.
Read more >
dio | Dart Package - Pub.dev
A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File downloading, Timeout etc. Get started #.
Read more >
[Solved] Null check operator used on a null value
In this example, we are going to show you how to show you the cause and solution of the "Null check operator used...
Read more >
Futures and error handling | Dart
Solution: Using Future.sync() to wrap your code. A common pattern for ensuring that no synchronous error is accidentally thrown from a function is...
Read more >
[SOLVED] Flutter : Cannot run with sound null safety, because ...
If you want run your project with --no-sound-null-safety so now you add this line your main.dart file in top(first line) with comment.
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