Giter Club home page Giter Club logo

dart-concepts's Introduction

Dart-Concepts

It contains some basic concepts of dart language that is needed for flutter app developement.

Factory Constructor

A factory constructor is a special type of constructor in Dart that allows you to create objects without using the new keyword. Instead, you can use a named constructor to define how the object should be created.

How to Use the Factory Constructor

To use a factory constructor, you must first define the constructor as factory. Then, you can use the constructor to create objects just like any other constructor.

๐Ÿ“ Example:

class MyClass {
  final String name;
  final int age;
  
  MyClass(this.name, this.age);
  
  factory MyClass.fromMap(Map<String, dynamic> map) {
    final name = map['name'] as String;
    final age = map['age'] as int;
    return MyClass(name, age);
  }
}

Benefits of Using a Factory Constructor

The main benefit of using a factory constructor is that it allows you to control the object creation process. You can perform any necessary calculations or data validations before returning the object. This can help you write more efficient and error-free code.

๐Ÿš€ Another benefit is that it allows you to return a cached instance of an object instead of creating a new instance every time. This can be useful if you need to create many instances of an object and want to avoid the overhead of creating new instances.

Benefits:

  • โšก๏ธ Efficient object creation process.
  • ๐Ÿ›ก๏ธ Controlled data validations.
  • ๐Ÿ”„ Ability to return cached instances.

Dynamic Variable

var

var variable once assigned the type can not change.

  void main() {
  var a = 10;
  print(a);
  a = 'Bob';
  print(a);
  a = 'c';
  print(a);
  a = 1.00012;
  print(a);
}

The above code will show error.

dynamic

dynamic variable once assigned the type can be changed.

void main() {
  dynamic a = 10;
  print(a);
  a = 'Bob';
  print(a);
  a = 'c';
  print(a);
  a = 1.00012;
  print(a);
}

But this code will run. Here is the output:

10
Bob
c
1.00012

Dart 3 New concepts

Record

void main() {
  final values = giveMeSomeDoubles();
  print(values.point);
  print(values.greeting);
}

({double point, String greeting}) giveMeSomeDoubles() {
  return (point: 4.5, greeting: 'Hey!');
}

Output:

4.5
Hey!

Pattern Matching

final list = [1, 2, 3];
final [a, b, c] = list;
print('$a $b $c');

Output:

1 2 3

What if, we need more variable for more value
or we need to skip some value. Below is the code for both cases...

final list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
final [a, b, c, ...d] = list;
print('$a $b $c $d');

Output

1 2 3 [4, 5, 6, 7, 8, 9]
final list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
final [a, _, c, ...d] = list;
print('$a $c $d');

Here b is skipped.
Output:

1 3 [4, 5, 6, 7, 8, 9]

With class related stuff.

void main() {
  final human = Human('Nice Name', 2);
  final Human(:name, :age) = human;
  print(name);
  print(age);
}

class Human {
  final String name;
  final int age;
  Human(this.name, this.age);
}

Output:

Nice Name
2

MOdified Switch Cases

List<String> listItems = ['HI', 'man'];
switch (listItems) {
  case ['Hi' || 'HI', 'man' || 'MAN']:
    print('matched');
}

Output:

matched

dart-concepts's People

Contributors

muntasir89 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.