pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
966 stars 190 forks source link

Positional optional parameters of entities constructor #175

Open luis901101 opened 5 years ago

luis901101 commented 5 years ago

With floor: ^0.7.0 and floor_generator: ^0.7.0 I can not use positional optional parameters in my entity constructors... the code generator always assume the parameters will be defined in the constructor... it will be great if the code generator for entities behaves like json_serializable builder... which accepts positional optional parameters and if constructor is empty then uses the individual set for each entity field...

By the way great lib, thanks for it, and keep improving it

vitusortner commented 5 years ago

Thanks for your participation! This is planned for the first stable release of the library. As you said, I've thought about applying a similar mapping to what json_serializable uses. Thus, the implementation shouldn't be too complicated, as there is an example (json_serializable) already.

luis901101 commented 5 years ago

Ok I'll be aware of the first stable version. Thank you

vitusortner commented 4 years ago

Can you describe your use case in more detail, please? Adding a code sample could also be beneficial.

luis901101 commented 4 years ago

There are several use cases that can benefit from positional optional parameters in constructors... for instance an entity that has some fields that are not required lets think about an entity Place which has an id(required not null), a latitude(required not null), a longitude(required not null), but a description(not required nullable)... it’s simple, i think positional optional parameters are useful when entity has not required fields. By the way as I mentioned above it would be great if the constructor is not required to define parameters for entity fields, I mean floor generator should use fields setters if the field is not set in the constructor. Something similar to json_serializable builder...

marcosmko commented 4 years ago

Hello!

I would also like to have this feature, it is more easy to have the generator automatically do it instead of us needing to add parameter to constructor every time we add a new variable to the entity. Cracked my head every time when getting null values 🤪

Also, I will complement the initial request with some examples, to better clarify this feature.

When we declare an entity, we do it something like this:

import 'package:floor/floor.dart';

@Entity()
class MyObject {
  MyObject(this.id, this.name, this.value);

  @PrimaryKey()
  int id;

  String name;
  String value;
}

which in turns generate this when fetching from database:

static final _myObject Mapper = (Map<String, dynamic> row) => MyObject(
      id: row['id'] as int,
      name: row['name'] as String,
      value: row['value'] as String;

We must declare all variables we want to be filled in the constructor, but some people (like me) tends to forget to do this and crack their heads trying to discover why they are getting null values when accessing variables.

The idea is to make the floor generator do this automatically for us, json_serializable style:

@Entity()
class MyObject {
  MyObject(this.id, {this.name});

  @PrimaryKey()
  int id;

  String name;
  String value;
}

static final _myObject Mapper = (Map<String, dynamic> row) => MyObject(
      row['id'] as int,
      name: row['name'] as String,
      )..value = row['name'] as String;