Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using reflection


The Dart mirror-based reflection API (contained in the dart:mirrors library) provides a powerful set of tools to reflect on code. This means that it is possible to introspect the complete structure of a program and discover all the properties of all the objects. In this way, methods can be invoked reflectively. It will even become possible to dynamically evaluate code that was not yet specified literally in the source code. An example of this would be calling a method whose name was provided as an argument because it is looked up in the database table.

Getting ready

The part of your code that uses reflection should have the following import code:

import 'dart:mirrors';

How to do it...

To perform reflection we perform the following actions:

  • In the reflection project, we use a class Embrace to reflect upon:

    void main() {
        var embr = new Embrace(5);
        print(embr);  // Embraceometer reads 5
        embr.strength += 5;
        print(embr.toJson()); // {strength: 10}
        var embr2 = new...