dart_codable 0.2.0 copy "dart_codable: ^0.2.0" to clipboard
dart_codable: ^0.2.0 copied to clipboard

Implementing the Swift Codable Protocol. It provides the abstract classes Encodable, Decodable and Codable to extend a custom class with.

dart_codable #

Implementing the Swift Codable Protocol.

Example #

First of all import the package alongside the dart:convert package.

import 'dart:convert';
import 'package:dart_codable/dart_codable.dart';

Have your Class extend the Codable Class. Note that you have to pass down the constructor from the superclass because dart does not automatically create non-default constructors.

class Person extends Codable{
  Person.decode(Map<String, dynamic> data) : super.decode(data);

  String name;
  int age;
  double score;
}

Then simply use it:

String jsonString = "{ \"name\": \"Marco\", \"age\": 24, \"score\": 99.9 }";

Person me = Person.decode(json.decode(jsonString));

print("Name: ${me.name}");
print("Age: ${me.age}");
print("Score: ${me.score}");
print("");

print(json.encode(me.encode()));

Which outputs

Name: Marco
Age: 24
Score: 99.9

{"name":"Marco","age":24,"score":99.9}
5
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Implementing the Swift Codable Protocol. It provides the abstract classes Encodable, Decodable and Codable to extend a custom class with.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

Dependencies

reflectable

More

Packages that depend on dart_codable