Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to serialize data from Firebase into Dart object for Flutter?

What is the best way to serialize a list of data from Firebase? Firebase provides an object with a list of properties for the list which makes it more challenging to come up with a good conversion technique.

How would you serialize this data from Firebase:

{
    "-KiRg_F-qC59xxlfZ6ej": {
        "first":"Brandon",
        "last":"Donnelson"
    },
    "-KiRgmsISBsJSWfXhrdD": {
        "first":"Danny",
        "last":"Kirk"
    }
}

What I came up with — see _loadData()) —:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Text(
          'click',
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _test,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),);
  }

  void _test() {
    _loadData();
  }

  _loadData() async {
    String url = 'https://dev-xxxxxxx.firebaseio.com/names.json';
    var httpClient = createHttpClient();
    var response = await httpClient.read(url);

    print('response=' + response);
    // response = {
    //     "-KiRg_F-qC59xxlfZ6ej":{"first":"Brandon","last":"Donnelson"},
    //     "-KiRgmsISBsJSWfXhrdD":{"first":"Danny","last":"Kirk"}
    // }

    NamesData namesData = new NamesData(JSON.decode(response));

    print("names.len=" + namesData.names.length.toString());
  }
}

class NamesData {
  final List<NameData> names = new List();

  NamesData(Map data) {
    data.values.forEach((Map map) => names.add(new NameData.fromJson(map)));
  }
}

class NameData {
  String first;
  String last;

  NameData.fromJson(Map map) {
    first = map['first'];
    last = map['last'];
  }
}

I found the JSON decoder has a better method for instantiating classes with the reviver function. This feels much better, but I think I can do better.

_loadData() async {
    String url = 'https://dev-xxxxxxx.firebaseio.com/names.json';
    var httpClient = createHttpClient();
    var response = await httpClient.read(url);

    print('response=' + response);
    // response = {
    //     "-KiRg_F-qC59xxlfZ6ej":{"first":"Brandon","last":"Donnelson"},
    //     "-KiRgmsISBsJSWfXhrdD":{"first":"Danny","last":"Kirk"}
    // }

    var extendedJson =  new JsonCodec(reviver: _reviver);

    var o = extendedJson.decode(response);

    print('end');
  }

  // https://github.com/dart-lang/sdk/blob/master/tests/lib/convert
  // /json_toEncodable_reviver_test.dart
  _reviver(key, value) {
    if (value != null && value is Map && key.toString().contains("-")) {
      return new NameData2(value);
    }
    return value;
  }

}

class NameData2 {
  String first;
  String last;

  NameData2(Map map) {
    first = map['first'];
    last = map['last'];
  }
}
like image 433
Brandon Avatar asked Apr 24 '17 01:04

Brandon


1 Answers

I personally like writing a tiny Codec sometimes:

DartPad example

import 'dart:convert';

void main() {
  final decoder = const FirebaseNamesDecoder();
  print(decoder.convert(exampleFirebaseData));
}

class NamedData {
  final String id;
  final String firstName;
  final String lastName;

  const NamedData(this.id, this.firstName, this.lastName);

  @override
  String toString() => '$NamedData {$id: $firstName $lastName}';
}

class FirebaseNamesDecoder extends Converter<Map, Iterable<NamedData>> {
  const FirebaseNamesDecoder();

  @override
  Iterable<NamedData> convert(Map<String, Map> raw) {
    return raw.keys.map((id) => new NamedData(id, raw[id]['first'], raw[id]['last']));
  }
}

final exampleFirebaseData = {
  "-KiRg_F-qC59xxlfZ6ej": {
    "first":"Brandon",
    "last":"Donnelson"
  },
  "-KiRgmsISBsJSWfXhrdD": {
    "first":"Danny",
    "last":"Kirk"
  }
};

Results in:

( NamedData {-KiRg_F-qC59xxlfZ6ej: Brandon Donnelson}, NamedData {-KiRgmsISBsJSWfXhrdD: Danny Kirk} )

Dart 2 needs modification to the overridden method:

    Iterable<NamedData> convert(Map<dynamic,dynamic> raw) {
    return raw.keys
        .map((id) => new NamedData(id, raw[id]['first'], raw[id]['last']));
  }
like image 129
matanlurey Avatar answered Oct 14 '22 11:10

matanlurey