How to generate a dart client from swagger json with nullsafety turned on?

1,115

Solution 1

You can use swagger_dart_code_generator (as of now 2.1.2) from https://github.com/epam-cross-platform-lab/swagger-dart-code-generator which supports null safety. The only issue with this that I observed at this point is that all properties are nullable, so basically the client code is bloated with null checks, exactly what the null-safety wanted to avoid. Not sure whether this is coming from the generator itself or from the chopper_generator (as of now 4.0.1) it uses. I also came to this thread trying to find out how to solve this inconvenience.

Solution 2

You'll only be able to generate the null-safe client once the generator is updated to generate null-safe code.

As you mentioned there are no issues on the repository, a good place to start would be to create the issue and find out the authors inclination towards updating the code to be null safe.

Null-safety has been in beta but has only recently been added to stable, the general practice here was to gradually migrate to null safety as this change has repercussions throughout the codebase. For example build_config: ^1.0.0 which is published by the core dart team published support for null safety in April 20th 2021, only a few days ago at the time of this post.

Share:
1,115
nvoigt
Author by

nvoigt

Updated on December 29, 2022

Comments

  • nvoigt
    nvoigt over 1 year

    I have a backend service that is producing a swagger.json as a description of their interface.

    I want to connect to that service, so up to now the easiest way has been to use a generator (for example swagger-gen or the OpenAPI variant) to generate this code. For Dart you even have choices in generation based on which HTTP client you want. Great. Worked well.

    Now Flutter is only my hobby, I use other languages and Frameworks at work. I loved the idea of null-safety and I really liked seeing it rolled out into production in this style. People are picking up, packages getting adapted. I love it.

    However, it seems after months and months of null-safety, beta, master, dev, stable, there is no movement, not github issues, not pull requests to get those generators to generate null-safe code. I have seen the packages that make working with the generators easier adopt null-safety for their own code, but the generated code still fails in recent Dart versions. It's still Dart 2.0 from 2018.

    So what am I missing here? Surely there are production apps out there that connect to backends? Did I miss a secret switch that has been there all the time? Is swagger maybe not used any more in the Flutter/Dart ecosystem, do all the professional app developers use something else nowadays?

    I'm used to swagger/OpenAPI, but I have full control of the backend, if something else is the latest best way of doing it, I can change the backend.

    How do I generate null-safe client Dart code from a backend service interface file?

  • nvoigt
    nvoigt about 3 years
    So what do all the professionals do right now? As I said, with Dart I'm a hobbyist that started their first new product a year after first dabbling with null-safety. I have a hard time imagining that other people that do this as a day job are just like "yeah, whatever, never liked connecting to backends anyway, it can wait another year".
  • Rohan Thacker
    Rohan Thacker about 3 years
    Waiting and planning the migration, you don't have to use null-safety at this time to work with flutter or dart. You can use earlier stable versions of packages.