Working with Flutter and Swagger API codegen

Daddycat @ Yinsee
2 min readApr 13, 2019

--

the missing pieces, when Swagger don’t play nice

Hi Flutter devs! This article is particularly useful for you if you use Swagger for your API.

In case you don’t realise this, Swagger.io generates beautiful API client code. By defining your API in swagger and using the code-generator, you save hundreds of hours writing API codes.

However the current dart generator is not compatible with the latest Flutter

I have done this so many times that i have to chart this down somewhere to preserve my memory.

First of all, add the http dependency to your project’s pubspec.yaml

Next find and edit api.dart, replace whatever you see in between the library … and part ‘…’ lines.

library swagger.api;

import ‘dart:async’;
import ‘dart:convert’;
import ‘dart:core’;
import ‘package:http/http.dart’;
import ‘package:http/io_client.dart’;
import ‘package:flutter/foundation.dart’;

part ‘api_client.dart’;

Edit api_client.dart, replace

var client = new BrowserClient();

to

var client = new IOClient();

Finally, A little detour while we are at this, to fix the query string not taking special characters. find ${p.name}=${p.value} and wrap ${p.value} with Uri.encodeQueryComponent(),

--

--