model_map 0.2.6

  • README.md
  • Installing
  • Versions
  • 17

ModelMap

Build Status

An experiment in using mirrors to convert from a map to a model and vice versa.

Introduction

Mirrors are a way of performing reflection in Dart. This permits an unknown class to be traversed and information about fields, properties and methods to be extracted, which is particularly useful if you wish to convert a serialized representation of an object such as JSON into an actual object instance.

Mirrors are now properly supported in dart2js, so model_map will work even if compiled to javascript as long as you decorate your entity with @reflectable.

At this stage ModelMap only worries about non-static, public fields and will ignore getters and setters.

Examples

Simple model

import 'package:model_map/model_map.dart';

class SimpleModel extends ModelMap
{
  String string;
  int integer;
  bool flag;
  num float;
}

main()
{
  var map   = { 'string': 'some text', 'integer': 42, 'flag': true, 'float': 1.23 };
  var model = new SimpleModel().fromMap(map);

  // The model is populated and ready to use at this point
}

You can also take an existing model and convert it to a map

// A map of <String, dynamic>
var map = model.toMap();

Using JSON

A couple of utility functions are included that simply wrap fromMap and toMap using the built-in parse and stringify capabilities so that you can simply call fromJson and toJson on your model instance.

Compiling to javascript

Now that dart2js supports enough of the mirror system, ModelMap will work in a javascript application. One thing to be aware of is that dart2js performs tree shaking and to ensure that your entities can be mirrored properly when compiled to javascript you should decorate them with a helper attribute:

@reflectable
class SimpleModel extends ModelMap
{
  String string;
  int integer;
}

Complex model

ModelMap can support model instances within models, but they must all extend the ModelMap class. It also handles List and Map collections (maps are limited to string keys only, since this is all that JSON can use).

import 'package:model_map/model_map.dart';

class ComplexModel extends ModelMap
{
  int integer;
  SimpleModel simple;
  List<SimpleModel> modelList;
  Map<String, DateTime> dateMap;
  Map<String, SimpleModel> modelMap;
  Map<String, List<SimpleModel>> modelListMap;
}

main()
{
  var json  = getJsonFromServer();
  var model = new ComplexModel().fromJson(json);

  print(model.modelMap['a key'].flag);
}

ModelMap should cope with reasonably complex object trees.

DateTime

When converting to JSON or a map, ModelMap will always convert dates to an ISO 8601 string, however when parsing a map it will accept an ISO 8601 string or an integer representing UTC unix time.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  model_map: "^0.2.6"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


      import 'package:model_map/model_map.dart';
  
Version Uploaded Documentation Archive
0.2.6 Jan 31, 2014 Go to the documentation of model_map 0.2.6 Download model_map 0.2.6 archive
0.2.5 Dec 19, 2013 Go to the documentation of model_map 0.2.5 Download model_map 0.2.5 archive
0.2.4 Dec 19, 2013 Go to the documentation of model_map 0.2.4 Download model_map 0.2.4 archive
0.2.3 Nov 8, 2013 Go to the documentation of model_map 0.2.3 Download model_map 0.2.3 archive
0.2.2 Oct 10, 2013 Go to the documentation of model_map 0.2.2 Download model_map 0.2.2 archive
0.2.1 Sep 24, 2013 Go to the documentation of model_map 0.2.1 Download model_map 0.2.1 archive
0.2.0 Sep 20, 2013 Go to the documentation of model_map 0.2.0 Download model_map 0.2.0 archive
0.1.2 Apr 16, 2013 Go to the documentation of model_map 0.1.2 Download model_map 0.1.2 archive
0.1.1 Feb 6, 2013 Go to the documentation of model_map 0.1.1 Download model_map 0.1.1 archive
0.1.0 Jan 30, 2013 Go to the documentation of model_map 0.1.0 Download model_map 0.1.0 archive

Analysis

We analyzed this package on May 22, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.54.0
  • pana: 0.11.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
57 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
17
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:model_map/model_map.dart with components: mirrors.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 2 errors 4 hints.

    Strong-mode analysis of lib/model_map.dart failed with the following error:

    line: 51 col: 10
    The getter 'type' isn't defined for the class 'DeclarationMirror'.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use model_map.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=0.8.10+6 <2.0.0
Dev dependencies
unittest any