Road to

Majid Hajian

mhadaily

import 'package:flutter/material.dart';
MaterialApp(
   ThemeData(
        name: "Majid Hajian",
        location: "Oslo, Norway",
        description: '''
                Google Developer Expert
        	Passionate Software engineer, 
	        Community Leader, Author and international Speaker
         ''',
        main: "Head of DevRel at Invertase.io",
        homepage: "https://www.majidhajian.com",
        socials: {
          twitter: "https://www.twitter.com/mhadaily",
          github: "https://www.github.com/mhadaily"
        },
        author: {
          Pluralsight: "www.pluralsight.com/authors/majid-hajian",
          Apress: "Progressive Web App with Angular, Book",
          PacktPub: "PWA development",
          Udemy: "PWA development",
        }
        founder: "Softiware As (www.Softiware.com)"
        devDependencies: {
          tea: "Ginger", 
          mac: "10.14+",
        },
        community: {
          FlutterVikings: "Orginizer", 
          FlutterCommunity: "Admin/Lead",
          ...more
        },
        ),
       );

mhadaily

Find me on the internet by

Head of DevRel at Invertase

Majid Hajian

final speakerJson = {
  "name": "Majid",
  "age": 25,
  "active": true,
};

List<dynamic> getSpeaker(json) {
  return [
    json['name'] as String,
    json['age'] as int,
    json['active'] as bool,
  ];
}

main() {
  final speaker = getSpeaker(speakerJson);
  final name = speaker[0];
  final age = speaker[1];
  final active = speaker[2];
}

mhadaily

mhadaily

class Speaker {
  Speaker(this.name, this.age, this.active);
  final String name;
  final int age;
  final bool active;
}

Speaker getSpeaker(json) {
  return Speaker(
    json['name'] as String,
    json['age'] as int,
    json['active'] as bool,
  );
}
main() {
  final speaker = getSpeaker(speakerJson);
  print(speaker.name);
  print(speaker.age);
  print(speaker.active);
}

mhadaily

class Speaker {
  Speaker(this.name, this.age, this.active);
  final String name;
  final int age;
  final bool active;
  factory Speaker.fromJson(json) {
    return Speaker(
      json['name'] as String,
      json['age'] as int,
      json['active'] as bool,
    );
  }
}

main() {
  final speaker = Speaker.fromJson(speakerJson);
  print(speaker.name);
  print(speaker.age);
  print(speaker.active);
}

mhadaily

var record = ('first', a: 2, b: true, 'last');

(String, int, bool) getSpeaker(json) {
  return (
    json['name'] as String,
    json['age'] as int,
    json['active'] as bool,
  );
}

main() {
  final speaker = getSpeaker(speakerJson);
  final name = speaker.$1;
  final age = speaker.$2;
  final active = speaker.$3;
}

Records

mhadaily

(String, int, bool) getSpeaker(json) {
  return (
    json['name'] as String,
    json['age'] as int,
    json['active'] as bool,
  );
}

main() {
  final (name, age, active) = getSpeaker(speakerJson);
  print(name);
  print(age);
  print(active);
}

Patterns

mhadaily




({String name, int age, bool active}) getSpeaker(json) {
  return (
    name: json['name'],
    age: json['age'],
    active: json['active'],
  );
}

main() {
  final speaker = getSpeaker(speakerJson);
  final name = speaker.name;
  final age = speaker.age;
  final active = speaker.active;
}

Records

mhadaily





({String name, int age, bool active}) getSpeaker(json) {
  return (
    name: json['name'],
    age: json['age'],
    active: json['active'],
  );
}

main() {
  final (:name, :age, :active) = getSpeaker(speakerJson);
  print('$name, $age, $active');
}

Patterns

mhadaily





({String name, int age, bool active}) getSpeaker(json) {
  return (
    name: json['name'],
    age: json['age'],
    active: json['active'],
  );
}

main() {
  final (name: name1) = getSpeaker(speakerJson);
  print(name1);

  final (name, _) = ("Majid", 25);
  print(name);
}

Patterns

mhadaily





final json = {'speaker':  ['Majid', 25]};

Patterns

mhadaily





main() {
  final json = {'speaker': ['Majid', 25]};

  final        {'speaker': [name,    age]} = json;

  print('$name, $age'); // Majid, 25
}

Patterns

mhadaily





if (json is Map<String, Object?> &&
    json.length == 1 &&
    json.containsKey('user')) {
  var user = json['user'];
  if (user is List<Object> &&
      user.length == 2 &&
      user[0] is String &&
      user[1] is int) {
    var name = user[0] as String;
    var age = user[1] as int;
    print('User $name is $age years old.');
  }
}

If

mhadaily



main() {
  final json = {
    'speaker': ['Majid', 25]
  };

  if (json case {'speaker': [String name, int age]}) {
    print('valid json!');
  }
}

Switch/If

mhadaily

main() {
  final json = {
    'speaker': ['Majid', 25]
  };

  switch (json) {
    case {'speaker': [String name, int age]}:
      print('valid json!');
    default:
      print('invalid');
  }
}

Switch/If

mhadaily

main() {
  final json = {
    'speaker': ['Majid', 25]
  };

  final int customAge = 25;

  switch (json) {
    case {'speaker': [String name, int age]} when customAge == age:
      print('valid json!');
    default:
      print('invalid');
  }
}

Switch/If

mhadaily

main() {
  final json = {
    'speaker': ['Majid', 25]
  };

  final int customAge = 25;

  switch (json) {
    case {'speaker': [String name, customAge && int age]}:
      print('valid json!');
    default:
      print('invalid');
  }
}

Switch/If

mhadaily

ThemeData getTheme(ThemeMode mode) {
  switch (mode) {
    case ThemeMode.light:
      return ThemeData.light();
    case ThemeMode.dark:
      return ThemeData.dark();
    default:
      return ThemeData.light();
  }
}

ThemeData getTheme(ThemeMode mode) {
  if (mode == ThemeMode.light) {
      return ThemeData.light();
  }
  if(mode == ThemeMode.dark){
      return ThemeData.dark();
  }
  return ThemeData.light();
}

Switch/If

mhadaily






ThemeData getTheme(ThemeMode mode) {
  switch (mode) {
  	
    ThemeMode.light => ThemeData.light(),
    ThemeMode.dark => ThemeData.dark(),
  }
}

Switch/If

mhadaily





(Color textColor, Brightness brightness) getThemeSettings(mode) {
  return switch (mode) {
    ThemeMode.light => (Colors.white, Brightness.light),
    ThemeMode.dark => (Colors.black, Brightness.dark),
    _ => throw Error()
  };
}

final (textColor, brightness) = getThemeSettings(ThemeMode.dark);

Switch/If

mhadaily

import 'package:flutter/material.dart';
void main() => runApp(
      MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: Center(
            child: FutureBuilder(
              future: Future<String>.delayed(
                const Duration(seconds: 3),
                () => 'Hello World!',
              ),
              builder: (context, snapshot) => switch (snapshot) {
                (AsyncSnapshot s) when s.hasData => Text(s.data!),
                (AsyncSnapshot s) when !s.hasError =>
                  const CircularProgressIndicator.adaptive(),
                (_) => const Text('Error'),
              },
            ),
          ),
        ),
      ),
    );

Flutter

mhadaily

import 'package:flutter/material.dart';
void main() => runApp(
      MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: Center(
            child: FutureBuilder(
              future: Future<String>.delayed(
                const Duration(seconds: 3),
                () => 'Hello World!',
              ),
              builder: (context, snapshot) => switch (snapshot) {
                AsyncSnapshot(:final data) => Text(s.data),
                (AsyncSnapshot s) when !s.hasError =>
                  const CircularProgressIndicator.adaptive(),
                (_) => const Text('Error'),
              },
            ),
          ),
        ),
      ),
    );

Flutter

mhadaily

class AuthException implements Exception {}
class EmailAlreadyInUseException extends AuthException {
  EmailAlreadyInUseException(this.email);
  final String email;
}
class WeakPasswordException extends AuthException {}
class WrongPasswordException extends AuthException {}
class UserNotFoundException extends AuthException {}


String describe(AuthException exception) {
  switch (exception) {
    case EmailAlreadyInUseException():
      return 'Email already in use';
    case WeakPasswordException():
      return 'Password is too weak';
    case WrongPasswordException():
      return 'Wrong password';
    case UserNotFoundException():
      return 'User not found';
    default:
      return 'Unknown exception';
  }
}

mhadaily

class AuthException implements Exception {}
class EmailAlreadyInUseException extends AuthException {
  EmailAlreadyInUseException(this.email);
  final String email;
}
class WeakPasswordException extends AuthException {}
class WrongPasswordException extends AuthException {}
class UserNotFoundException extends AuthException {}


String describe(AuthException exception) {
  return switch (exception) {
    EmailAlreadyInUseException(:var email) => 'Email already in use: $email',
    WeakPasswordException() => 'Password is too weak',
    WrongPasswordException() => 'Wrong password',
    _ => 'Unknown exception',
  };
}

main() {
  print(describe(EmailAlreadyInUseException('m@mj.com')));
}

mhadaily

sealed class AuthException implements Exception {}
class EmailAlreadyInUseException extends AuthException {
  EmailAlreadyInUseException(this.email);
  final String email;
}
class WeakPasswordException extends AuthException {}
class WrongPasswordException extends AuthException {}
class UserNotFoundException extends AuthException {}


String describe(AuthException exception) {
  return switch (exception) {
    EmailAlreadyInUseException(:var email) => 'Email already in use: $email',
    WeakPasswordException() => 'Password is too weak',
    WrongPasswordException() => 'Wrong password',
  };
}

main() {
  print(describe(EmailAlreadyInUseException('m@mj.com')));
}

mhadaily

dart.dev/language

mhadaily

Find me on the internet by

Head of DevRel at Invertase

Majid Hajian

Slides

slides.com/mhadaily

Road to Dart 3

By Majid Hajian

Road to Dart 3

  • 337