Simple JSON with Flutter

JSON handling in Flutter is the same as JSON handling in dart. For a more detailed description of some of the possibilities, see Using Dart with JSON Web Services.

Imports

A single import is required to get the native Dart JSON handling. Simply add this to any file that needs to parse JSON.

import 'dart:convert';

(de)serialization

The dynamic nature of dart makes deserializing JSON trivial

List decodedList = JSON.decode('["Flutter", true]');
// decodedList[0] == 'Flutter'
// decodedList[1] == true

Map decodedMap = JSON.decode('{"framework":"Flutter", "awesome":true}');
// decodedMap['framework'] == 'Flutter'
// decodedMap['awesome'] == true

Encoding JSON is just as simple:

var data = JSON.encode({
    'framework': "Flutter",
    'tags': ['flutter', 'snippets'],
    'versions': '0.0.20',
    'task': 13511,
});
// data == '{"framework":"Flutter","tags":["flutter","snippets"],"versions":"0.0.20","task":13511}'

GitHub

A simple example of parsing JSON with simple API calls can be found in the Flutter JSON API project.

Brian Armstrong

Brian Armstrong

I'm Brian Armstrong, a SaaS developer with 15+ years programming experience. I am a Flutter evangelist and React.js enthusiast.