Dart4Flutter – 01 — Variables, types, and functions

Dart4Flutter — 02 — Control flow and anomalies

Dart4Flutter — 03 — Classes and generics

Dart4Flutter — 04 — async and library

Dart 4FLUTTER – gLEANer 01 – Flutter – DART environment setup

Dart4Flutter – immutability

Flutter Entry – State management

Introduction to Flutter Example 1

Description about the properties of Flutter Container

Flutter Entry – Local access -MethodChannel

Flutter instance – Load more ListViews

Flutter instance – Local to Flutter communication – Event Channels

No deformation has always been a hot topic, especially in the previous paragraph. Libraries such as immutable.js and other concepts such as one-way data flow argue that it is easier to manage data when it is Immutable.

In object-oriented and functional programming, an immutable object means that its state cannot be changed once the object is created. The object state can be changed after it is created.

Ok, so how does Dart do that? We have a few concepts that are good for built-in immutable and immutable objects, starting with const modifiers and const constructors. Not to be confused with const in ES6, which is just an immutable binding.

// define USER as constant and initialize.
const USER = { name: 'Joe'; }

// This will throw an exception.
USER = {};

// But it won't; The object can be modified as if it is bound immutable
USER.name = 'Jill';
Copy the code

In Dart, const is an immutable binding and immutable object.

main() {
  const user = const {'name': 'Joe'};
  
  // Throw Static error: "Constant variables cannot be assigned a value".
  user = {};
  
  // Raise Runtime error: "Unsupported operation: Cannot modify unmodifiable Map".
  user['name'] = 'Jill';
}
Copy the code

All types, such as Null, String, int, double, num, bool, Map, List, Symbol, can be immutable and const constructors can be added to custom types.

class User {
  final String name;
  
  const User(this.name);
}

main() {
  const user = const User('Joe');
}
Copy the code

Let’s recall – const instances are both immutable bindings and enforced at the language level, and are both fundamentally immutable – and determined at compile time – that is, any two instances are considered equivalent and can only be represented by a single instance at run time. For example, the following is fairly simple and only allocates one instance at run time:

class User {
  final String name;
  final List<String> cars;
  
  User(this.name, {this.cars});
}

main() {
// Loop 100 times, but only one object
  for (var i = 0; i < 100; i++) {
    const users = const {
      'Matan': const User(
        'Matan Lurey',
        cars: const [
          'Truck'.'Jeep'.'GoKart',])}; }}Copy the code

Refer to the official Dart documentation for more information on const and final

Check by package: Meta-static analysis

Of course, const is limited – the class object must be created at compile time. So you can’t do something like, at runtime, read data from the database and create objects. We now introduce the IMmutable annotation for Package :meta.

import 'pacakge:meta/meta.dart';
// Since the current class uses the @immutable annotation, variables must be final
@immutable
class User {
  final String name;
  User(this.name)
}
Copy the code

You can use this annotation to help developers enforce immutable classes. It’s not as formal as const, but it’s still useful to developers.

reference

  • Medium.com/dartlang/an…