Dart:mirror in flutter

2732 views
Skip to first unread message

Jorge Garcia Irazabal

unread,
Dec 17, 2016, 3:31:52 PM12/17/16
to flutt...@googlegroups.com
Hello,  I think flutter has a great potential but I would love to use dart:mirror so I can create a generic serialicer and been able to call a function from a string.

Do you plan to implement this library in the future? 

Thanks.

Adam Barth

unread,
Dec 17, 2016, 3:38:53 PM12/17/16
to Jorge Garcia Irazabal, flutt...@googlegroups.com
Hi Jorge,

We don't plan to add support for dart:mirror.  It's certainly a useful library, but it doesn't work well with the ahead-of-time compiler we use for Dart.  Depending on what you're trying to do, there might be ways to accomplish it without mirrors, although generic serialization might not be possible in a fully generic way, similar to how it is difficult to write a generic serializer in C++.

Adam


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matan Lurey

unread,
Dec 17, 2016, 5:46:03 PM12/17/16
to Adam Barth, Jorge Garcia Irazabal, flutt...@googlegroups.com
@Jorge:

There are various strategies to do this using ahead-of-time source code generation without using mirrors.

One of the members of the Dart team talked about his serialization generator on Medium last week:

Seth Ladd

unread,
Dec 17, 2016, 8:57:41 PM12/17/16
to Matan Lurey, Adam Barth, Jorge Garcia Irazabal, flutt...@googlegroups.com

Built_values looks cool. How would I use that and source_gen with Flutter's hot reload? Could I iterate on my model, trigger a source gen, and see my changes on the device? Does that even make sense?

Matan Lurey

unread,
Dec 17, 2016, 9:02:55 PM12/17/16
to Seth Ladd, Adam Barth, Jorge Garcia Irazabal, flutt...@googlegroups.com
Makes total sense. Today source_gen has several different ways that can be run - a very simple one is it just edits the filesystem, and I imagine it would work out of the box with Flutter and Hot Reload today (it essentially is just creating and editing .dart files that you'd later check in).

Steven Roose

unread,
Dec 18, 2016, 9:05:55 AM12/18/16
to Jorge Garcia Irazabal, flutt...@googlegroups.com

This has been discussed here as well: https://github.com/flutter/flutter/issues/1150

The only real argument against allowing mirrors is that it increased binary size. Which, in my opinion is a consideration that an application developer should be able to make for himself, ruled away by the platform.

But I fear that it will not be possible to convince the current Flutter team of that opinion, maybe when the team expands and people that share our opinion join, but don't count on it.

Matan Lurey

unread,
Dec 18, 2016, 8:10:56 PM12/18/16
to Steven Roose, Jorge Garcia Irazabal, flutt...@googlegroups.com
Steven I think the Flutter team asked a few times in that thread for examples where you feel you need mirrors.

It's hard to justify "please add feature X [because I want it]" - there are certainly an infinite amount of those requests.

If they can have some ideas of what you intend to use it for (and why other alternatives are not OK) I think it will be easier to have a discussion around either adding mirrors support or finding an alternative that will make you productive without adding support.

martin...@gmail.com

unread,
Sep 19, 2017, 5:31:54 PM9/19/17
to Flutter Dev
Hello,

I'd like to avoid using mirrors, but what about ORM which needs to reflect an object to store/restore it to/from a database? Is it possible to do this without reflection? I spent a lot of time trying to make it working - I've chosen dart_orm (https://pub.dartlang.org/packages/dart_orm) and created SQLite adapter for it just to find out it's not possible to use native SQLite in Flutter (my fault), so I created another adapter to use sqflite (SQFLite is a bridge between Flutter and Android/iOS SQLite, https://github.com/tekartik/sqflite).

Then build failed on dart_orm as it uses mirror :(
E/flutter (11085): [ERROR:lib/tonic/logging/dart_error.cc(16)] 'package:dart_orm/src/operations.dart': error: line 4 pos 1: import of dart:mirrors with --enable-mirrors=false
E/flutter (11085): import 'dart:mirrors';

So I'm curious if there is a way to implement ORM in Dart/Flutter.

Martin

Dne pondělí 19. prosince 2016 2:10:56 UTC+1 Matan Lurey napsal(a):

Matan Lurey

unread,
Sep 19, 2017, 7:17:21 PM9/19/17
to martin...@gmail.com, Flutter Dev
On Tue, Sep 19, 2017 at 2:31 PM <martin...@gmail.com> wrote:
Hello,

I'd like to avoid using mirrors, but what about ORM which needs to reflect an object to store/restore it to/from a database? Is it possible to do this without reflection?

[disclaimer: I work on the Dart team, and not on Flutter]

There are a couple options today:

1.) Write an API that relies on imperative (not declarative) schema configuration.

For example:

void main() {
  var schema = new Schema();
  schema.addTable('users', () => new User(...));
}

instead of

@ORM.DBTable('users')
class User extends ... {}

This has the downside of a linear amount of boilerplate, but for a project with just a few tables and up to several (but not 100s), this would scale quite well, and you'd get great debugging support (i.e. can use breakpoints, inspect code, profile - stuff that is really hard with reflection).

Incidentally, Flutter is a functional/imperative framework too, they also don't use declarative or aspect-oriented programming, so you'd be more like Flutter ;)

2.) Write an ahead-of-time processor/code generator that understands annotations.

For example, you'd have the following code:

part 'user.g.dart';

@ORM.DBTable('users')
class User implements ORM.Model {
  factory User() = _$User;
}

You can see an example of an e2e-package that does exactly this for JSON here:

This might be a great option if you're going to have lots of tables and/or you're beyond a prototype and want something that generates the boilerplate for you. 

Ultimately, there isn't anything I know about out-of-the-box that does this already for you, but the Dart and Flutter community is constantly growing, and you might be able to find others to collaborate with. 

Cheers!
Reply all
Reply to author
Forward
0 new messages