Dart Classes— Dart Programming — Part 5

class Point{
double? x;
double? y;
}
Point point1 = Point(2,4);
class Point{
double? x;
double? y;

Point(this.x, this.y);
// or with this way
Point(double x, double y) {
// See initializing parameters for a better way
// to initialize instance variables.
this.x = x;
this.y = y;
}
}
const double defaultX = 0;
const double defaultY = 0;

class Point {
final double x;
final double y;

Point(this.x, this.y);

// Named constructor
Point.origin()
: x = defaultX,
y = defaultY;
}
class Point {
double x, y,z;

// The main constructor for this class.
Point(this.x, this.y,this.z);

// Delegates to the main constructor.
Point.alongXAxis(double x) : this(x, 0,0);
}
class ImmutablePoint {
static const ImmutablePoint origin = ImmutablePoint(0, 0);

final double x, y;

const ImmutablePoint(this.x, this.y);
}
class Logger {
final String name;
bool mute = false;

// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache = <String, Logger>{};

factory Logger(String name) {
return _cache.putIfAbsent(name, () => Logger._internal(name));
}

factory Logger.fromJson(Map<String, Object> json) {
return Logger(json['name'].toString());
}

Logger._internal(this.name);

void log(String msg) {
if (!mute) print(msg);
}
}
var logger = Logger('UI');
logger.log('Button clicked');

var logMap = {'name': 'UI'};
var loggerJson = Logger.fromJson(logMap);
import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable()
class Person {
final String firstName, lastName;

final DateTime? dateOfBirth;

Person({required this.firstName, required this.lastName, this.dateOfBirth});

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

Map<String, dynamic> toJson() => _$PersonToJson(this);
}
import 'dart:math';

class Point {
final double x;
final double y;

Point(this.x, this.y);

double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
Point point1 = Point(2,4);
double distance = point1.distanceTo(Point(4,8));
class Point {
final double x;
final double y;

Point(this.x, this.y);
double get plusPoint => x * y; void set changeY(double newPoint) => y = y + newPoint; double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
abstract class Car{
void getEngine();
}
class SportCar extends Car {
@override
void getEngine(){
print("Sport engine");
}
  • Return type must be same types as the overriden method’s
  • Agrument type must be the same type as overriden method’s agrument types.
  • If the overridden method accepts n positional parameters, then the overriding method must also accept n positional parameters.
  • A generic method can’t override a non-generic one, and a non-generic method can’t override a generic one.

Extension methods

extension CurrencyFormat on number{
String get twoDecimalCurrency => "${this.toStringAsFix(2)} $";
}String moneyCurrency = 10.twoDecimalCurrency;

Static variables & Static methods

class Point {
static const defaultX = 9;
static const defaultY = 9;
}double defaultX = Point.defaultX; //Without initialize constructor
class Point {
double x, y;
Point(this.x, this.y);

static double distanceBetween(Point a, Point b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
}
void main() {
var a = Point(2, 2);
var b = Point(4, 4);
var distance = Point.distanceBetween(a, b);
assert(2.8 < distance && distance < 2.9);
print(distance);
}

Enumerated types

enum Car {
sportCar,
practiceCar,
other,
}
assert(Car.sportCar.index == 0);
List<Car> cars = Car.values;
Car currentCar = Car.sportCar;switch (currentCar) {
case Car.sportCar:
print('sport car!');
break;
case Car.practiceCar:
print('practice car!');
break;
case Car.other:
print('other car!');
break;
default: // Without this, you see a WARNING.
print(currentCar); //
}
enum Car {
sportCar,
practiceCar,
other,
}
Car currentCar = Car.sportCar;
// Before Dart 2.16
print("$currentCar"); // it'll return Car.sportCar
// After Dart 2.16
print("${currentCar.name}"); // it'll return sportCar
Actually before Dart 2.16 we can write an extension to get value from enum like this.extension CarExt on Car{
String get name => this.toString().split(.).last;
}
But very appriciate that Dart have support it. So try using lastest version.
  • You can’t explicitly instantiate an enum.

Mixins

--

--

--

I've started with Flutter since 2019 and have good knowledge about Flutter and some architecture. I want to share more of my knowledge and learn more from other

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Secrets of coding style

What is created with create-react-app?

https://opensea.io/collection/monkeyart3

Evolution of a Node.Js API, Zoe.Js — Introduction

Hello API

Reading CSV file in Postman

Binary Search: Using Multiple Pointers

A Guide to Currying in JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hoang Nguyen

Hoang Nguyen

I've started with Flutter since 2019 and have good knowledge about Flutter and some architecture. I want to share more of my knowledge and learn more from other

More from Medium

Flutter Login Tutorial with (Validation, Live Data, @Freezed Models, Secure Local Storage) — Part 2

How to call a function from another widget | Flutter Tutorial

GoRouter and Flutter Navigation 2 supporting.

How to Get User’s Current Location Address in Flutter — Geolocator & Geocoding