Skip to content

Instantly share code, notes, and snippets.

Last active February 13, 2022 02:15
What would you like to do?
Dio Built Value Json Decoder/Transformer for Dart/Flutter
import 'dart:convert';
import 'package:my_app/data_model/serializers.dart';
import 'package:flutter/foundation.dart';
Future<T> decodeJson<T>(String res) async {
var list = List();
//Replace compute with spawning any other isolate, compute is simpler abstraction of isolate api.
var result = await compute(_decode, list);
return result;
dynamic _decode(List list) {
var decoded = json.decode(list[0]);
var matchedDecoder = serializers.serializerForType(list[1]);
if (matchedDecoder != null) {
return serializers.deserializeWith(matchedDecoder, decoded);
} else {
return null;
// Sample usage
Future<User> getUser() async {
try {
Response<String> response = await _dio.get("/me");
var userResponse = await decodeJson<User>(;
return userResponse;
} catch (error, stacktrace) {
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment