functional_data 0.2.2

  • Installing
  • Versions
  • new72

functional_data #

Simple and non-intrusive code generator for boilerplate of data types. The package generates a simple mixin with operator==, hashCode, copyWith, toString, as well as lenses.

Boiler plate #

Because the boiler plate is generated as a mixin, it is minimally intrusive on the interface of the class. You only have to provide a constructor with named arguments for all fields and extend the generated mixin.

class Person extends $Person {
  final String name;
  final int age;
  Person({, this.age});
  Person.anonymous() : this(name: "John Doe", age: null);
  int get ageInDays => 356 * age;

Because of this design, you have complete control over the class. You can, for example, add named constructors or methods to the class like you're used to.

Using #

To use functional_data, add the following dependencies to your package:



And run flutter packages pub run build_runner build lib to generate code.

Lenses #

For every class, lenses are generated for all fields which allow viewing a field or creating a new instance of the classes with that field modified in some way. For example, the lens of Person's name is Person$.name. To focus a lens on a specific instance use its of method:

final teacher = Person(name: "Arthur", age: 53);

// -> "Arthur"

// -> Person(name: "Arthur", age: 60)

print(Person$.name.of(teacher).map((name) => name.toUpperCase()));
// -> Person(name: "ARTHUR", age: 53)

This isn't very exciting yet. The power of lenses comes to light when you combine them. It allows you to easily create a copy of a large nested data structure with one of the fields in a leaf modified. Two lenses can be chained using then.

class Course extends $Course {
  final String name;
  final List<Person> students;

final programming = Course(name: "Programming 101", students: [Person(name: "Jane", age: 21), Person(name: "Tom", age: 20)]);

final firstStudentsName = Course$.students.then(List$.first<Person>()).then(Person$.name);

// -> Course(students: [Person(name: "Marcy", age: 21), Person(name: "Tom", age: 20)]

Compare this with the alternative:

final firstStudent = programming.students.first;
final updatedFirstStudent = Person(name: "Marcy", age: firstStudent.age);
final updatedStudents = [updatedFirstStudent] + programming.students.skip(1);
final updatedCourse = Course(name:, students: updatedStudents);

This is much less readable and error prone. Imagine what happens when one of the classes gains a field.

Full example: #

// lens.dart
import 'package:collection/collection.dart';

import 'package:functional_data/functional_data.dart';

part 'lens.g.dart';

// Only requirement is that it has a constructor with named arguments for all fields
class Foo with $Foo {
  final int number;
  final String name;

  // Can't be const because of mixin

class Bar with $Bar {
  final Foo foo;

  final List<Foo> foos;

  final String driver;

  Bar({, this.foos, this.driver});

void main() {
  final foo = Foo(number: 42, name: "Marvin");
  final bar = Bar(foo: foo, foos: [Foo(number: 101, name: "One"), Foo(number: 102, name: "Two")], driver: "One");

  final fooName = Bar$.foo.then(Foo$.name);
  // print( => name.toUpperCase(), bar));
  print(fooName.of(bar).map((name) => name.toUpperCase()));
  // Bar(foo: Foo(number: 42, name: MARVIN), foos: [Foo(number: 101, name: One), Foo(number: 102, name: Two)], driver: One)

  final firstFooName = Bar$.foos.then(List$.atIndex<Foo>(0)).then(Foo$.name);
  // print(firstFooName.update(bar, "Twee"));
  // Bar(foo: Foo(number: 42, name: Marvin), foos: [Foo(number: 101, name: Twee), Foo(number: 102, name: Two)], driver: One)

  final nameOfFooNamedTwo = Bar$.foos.then(List$.where<Foo>((foo) => == "Two")).then(Foo$.name);
  print(nameOfFooNamedTwo.update(bar, "Due"));
  // Bar(foo: Foo(number: 42, name: Marvin), foos: [Foo(number: 101, name: One), Foo(number: 102, name: Due)], driver: One)

  final driversNumber =
      Bar$.foos.thenWithContext((bar) => List$.where<Foo>((foo) => == bar.driver).then(Foo$.number));
  // 101

0.2.2 #

  • Add instructions for using functional data in README

0.2.1 #

  • Add changelog
  • Format code
  • Add description

0.2.0 #

  • First published version

Use this package as a library

1. Depend on it

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

  functional_data: ^0.2.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

import 'package:functional_data/functional_data.dart';
Version Uploaded Documentation Archive
0.2.2 Feb 3, 2019 Go to the documentation of functional_data 0.2.2 Download functional_data 0.2.2 archive
0.2.1 Feb 3, 2019 Go to the documentation of functional_data 0.2.1 Download functional_data 0.2.1 archive
0.2.0 Feb 3, 2019 Go to the documentation of functional_data 0.2.0 Download functional_data 0.2.0 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:functional_data/functional_data.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and functional_data.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
collection ^1.14.11 1.14.11
meta ^1.1.6 1.1.7