Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution
From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution
From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution
Ebook2,143 pages2 hours

From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book is more than a set of instructions; it's an invitation to embark on a collaborative journey through the thrilling realm of platform-agnostic application development using Flutter. Together, we'll explore every phase of the application lifecycle, starting from the initial concept to the final distribution, with essential stops at development, testing, and automation along the way. This collaborative learning experience promises to be both exciting and educational as we delve deeper into this process as reading through.

The goal of this book is not to teach how to create applications; it empowers to become a proficient, well-rounded developer.
LanguageEnglish
PublisherLulu.com
Release dateOct 25, 2023
ISBN9781446690000
From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution

Related to From Zero to Market with Flutter

Related ebooks

Programming For You

View More

Related articles

Reviews for From Zero to Market with Flutter

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    From Zero to Market with Flutter - Viachaslau Lyskouski

    From Zero to Market with Flutter

    Desktop, Mobile, and Web Distribution

    Viachaslau Lyskouski, 2023

    PIC

    Dedicated to my Family


    The purpose of this book is not merely to instruct but to embark on a shared journey into the realm of platform-agnostic application development using Flutter. I’ve started that book by knowing nothing about Flutter and Dart, and the spent time have given me just an initial impulse to the mastery, but I still have something to share with you.

    I am confident that the time spent on coding (approximately 200 hours) can suffice for grasping the fundamental concepts of any programming language or framework, regardless of your prior background, as long as you progressively tackle more complex tasks while gradually reducing the need for assistance.

    My approach to learning has evolved into a day-to-day habit, which I’ve diligently followed over the past 20 years while working as a full-stack developer. My technical proficiency is complemented by a profound customer focus and business acumen. That possess insights into a product, project, and software life cycles.

    I warmly invite you to join this project as it unfolds throughout the pages of this book. Together, we will embark on an exploration of Flutter and its extensive capabilities. This collaborative learning journey promises to be both exciting and enriching as we delve further into the depths of this versatile framework.

    © Viachaslau Lyskouski, 2023: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)

    ISBN: 978-1-4466-9000-0

    Imprint: Lulu.com

    Contents

    Introducing

    Bootcamping

    Dart

    Flutter

    Conceptualizing

    Simplifying User Interface

    Hierarching Information

    Onboarding

    Personalizing Options

    Securing Information

    Visualizing Data

    Integrating Services

    Shaking the Market

    Enhancing Continually

    Prototyping

    Configuring Environment

    Unlocking Multi-Platform

    Creating Skeleton

    Defining Storage

    Enriching Interface

    Activating Sponsorship

    Gating

    Testing Quality

    Automating Conditions

    Adding Telemetry

    Configuring Deployment

    Assessing of Ignorance

    Unleashing

    Benchmarking Prototype

    Refactoring by Principles

    Visualizing Data

    Aggregating External Sources

    Replicating Data

    Optimizing

    Unifying Stylistic

    Managing Attention

    Relying Versatility

    Anticipating Experience

    Conducting Usability Tests

    Supporting Accessibility

    Handling Instabilities

    Productionizing

    Visioneering

    Securing Information

    Researching

    Marketing

    Distributing

    Mobile

    Desktop

    Web

    Continuing

    References

    Bibliography

    Introducing

    Dear readers, I invite you to join me on a journey into a platform-agnostic application development, starting from ground zero. Meaning, that we (me and, possibly, you) might know nothing about cross-platform or mobile development, ‘Flutter‘-framework, ‘Dart‘-language, by not having even an idea for the application; ideally, it might be chosen a market, which to shake (in our case, – financial accounting).

    That book might be misaligned with the concepts that you’re following, respecting, or adepting; by representing the author’s vision, that’s why I invite you to argue with me (and kindly request your assistance in identifying possible errors and misprints) on the pages of a repository that contains the sources of this book and an application that we’ll develop together as reading through https://github.com/lyskouski/app-finance. And... who knows, may be your arguments will change the ground ideas of that book, and we’ll republish it together. So, here are those statements:

    Creating any program is similar to apotteryworkshop flow, by taking amorphousidea and shaping it. Through the timeit can be changed an architecturalstyle (homogeneous, layered, multitier,microservices,etc.), structure (monolithic, distributed,hierarchical, etc.),even platform (from platform specific toserverless), and language. It’s all aboutbeing Agile, being adaptive to all externaland internal challenges. Meaning, thatthe application’s revenue justifies anyrefactoring, including the possibility ofre-creating it from scratch.

    Programming is not something aboutcreativity, in its pragmatic meaning(no one, except the author, will beable to accurately obtain the sameresult if the same initial situation iscreated), but mastery (how accurate,quick, balanced, and far-sighted will beyour implementation).

    Instruments (languages, frameworks,libraries, etc.) should be adapted to solvethe problem instead of adapting initialideas to a capability of available / knowntools.

    Insteadof being ‘I‘, ‘T‘, ‘U‘, or ‘M‘-shaped,nowadays dictate us to be ‘_‘-shaped(underscore-shaped). While the mostscientists maintain a broad outlookwith a narrow specialty (‘T‘-shaped), asignificant portion of programmers standon an unstable foundation. Meaning thattechnologies are infinitely replacing eachother through thetime via tick-tock model (characterizedby periods of evolution and revolution)and the most effective approach is tocontinuously expand one’s fundamentalknowledge (polymathy, [Root09]) bystaying acquainted with the technologiesin use.

    In case of having any discrepancies with this, the book may cause a rejection; and it’s the best time to refund. Moreover, it is been started without any knowledge in mobile and cross-platform development, Flutter, Dart, and understanding the needs of financial accounting market. ‘ChartGPT‘ was used as a mentor, and ‘Midjourney‘ – to generate a few images (including covers of that book), and shared some design insights.

    Returning to the core themes of the book and the selected technologies, it’s evident that businesses aim to embrace innovative technologies to optimize their processes and provide outstanding user experiences, all while ensuring broad platform compatibility. Not so far, Flutter (Dec 4th, 2018 – version 1; May 10th, 2023 – version 3) has emerged as a popular choice for building high-performance, platform-agnostic (desktop, mobile, web) applications. And this book, From Zero to Market with Flutter: Desktop, Mobile, and Web Distribution, road us to a comprehensive journey through the creation of a robust competitive application by using Flutter with ‘no-server‘-basis (while ‘serverless‘ is mostly associated with cloud-based infrastructure).

    The central objective of this book is to illustrate how to harness the extensive capabilities of Flutter to build a comprehensive and feature-rich application. Across the chapters, we will delve into every phase of the application development process, starting from conceptualization and culminating in distribution:

    Take basics in ‘Dart‘-language and‘Flutter‘-framework, and how it enablescross-platform development.

    Gain insights into the design principles ofan application development.

    Go through User Interface and Experiencepractices to create an intuitive userinterface.

    Discover how to incorporate essentialfeatures into the application with aforecast release planning.

    Explore strategies for securely interactingwith external systems and ensuring dataintegrity.

    Master techniques for testing (unit,widget, integration, and performancetests) and debugging practices.

    Configure the application distributionacross all platforms (Windows,Linux, MacOS, Android, iOS) to theirmarketplaces, and explore strategies forongoing maintenance and updates.

    So, let’s embark on this learning curve journey and keep ‘Flutter‘ing.

    Bootcamping

    Dart

    Dart is known for its simplicity and readability as object-oriented and class-based language. It offers C-style syntax and object-oriented programming (OOP) concept (inheritance, polymorphism, and encapsulation).

    Dart Virtual Machine (VM) includes a just-in-time (JIT) compiler for fast development cycles and an ahead-of-time (AOT) compiler for optimized production performance by translating ‘Dart‘-code into a native machine code. With a support of multi-threaded execution, VM provides efficient utilization of modern multi-core processors via ‘Isolates‘ (lightweight, independent workers that communicate via message passing) for concurrent executions.

    Dart Virtual Machine is designed as a platform-agnostic interpreter (desktop environments [Windows, macOS, Linux], mobile devices [Android, iOS], and web browsers) with a modular and extensible architecture (capabilities to extend core-components as primitives and classes). It includes components such as Dart Core Libraries, Garbage Collector (with separating objects into young and old generations, automatically reclaims memory occupied by objects that are no longer reachable), and Dart Development Compiler (DDC, designed to compile Dart to JavaScript / TypeScript by enabling real-time code changes and efficient debugging for web applications).

    Dart Software Development Kit (SDK) provides a command-line interface (CLI), a package manager, and a collection of utility libraries that simplify common tasks. Package Manager (‘pub‘) controls libraries and their dependencies via ‘pubspec.yaml‘-file by using public (https://pub.dev) and private (‘dart pub add private_package --hosted https://...‘) storages.

    Entire End-to-End Stack Dart is well-equipped to cover the full stack development. ‘Flutter‘, powered by Dart, enables developers to write code once and run it on multiple platforms, providing a unified user experience. ‘Aqueduct‘ provides a robust foundation for building scalable and high-performance backend services and APIs. Dart’s versatility extends seamlessly to a database layer, offering compatibility with a wide range of database systems, both SQL and noSQL databases (as ‘Aqueduct ORM‘ for PostgreSQL, ‘Realm‘ for MongoDB), as well as databases built specifically using Dart as their foundation (like ‘DartinoDB‘).

    Checking Primitives

    Primitives (built-in data types) behave as objects with own methods and properties, and can be even extended by ‘extension‘:

    01:

    int

     

    counter

     

    =

     

    123;

     

    //

     

    counter

    .

    toDouble

    ()

     

    =>

     

    123.0

     

    02:

    double

     

    pi

     

    =

     

    3.141592;

     

    //

     

    pi

    .

    clamp

    (0,

     

    3)

     

    =>

     

    3.0

     

    03:

    bool

     

    isVisible

     

    =

     

    true

    ;

     

    //

     

    true

    .

    toString

    ()

     

    =>

     

    true

     

    04:

    String

     

    text

     

    =

     

    Some

     

    content

    ;

     

    //

     

    text

    .

    codeUnits

     

    //

     

    List

    <

    int

    >

     

    05:

    dynamic

     

    tmp

     

    =

     

    1;

     

    tmp

     

    =

     

    test

    ;

     

    tmp

     

    =

     

    false

    ;

     

    //

     

    ...

     

    06:

    //

     

    StringBuffer

     

    as

     

    a

     

    way

     

    to

     

    construct

     

    strings

     

    sequentially

     

    07:

    final

     

    spread

     

    =

     

    StringBuffer

    ()

    ;

     

    08:

    spread

    .

    write

    (

    some

     

    text

    ...

    )

    ;

     

    //

     

    concatenate

     

    09:

    spread

    .

    writeln

    (

    ...

     

    another

     

    content

    )

    ;

     

    //

     

    add

     

    indentation

     

    at

     

    the

     

    end

     

    10:

    //

     

    But

     

    long

     

    text

     

    can

     

    be

     

    also

     

    broken

     

    and

     

    implicitly

     

    spliced

     

    back

     

    11:

    String

     

    test

     

    =

     

    some

     

    long

     

    text

    ...

     

    12:

     

    ...

    continuation

    ;

     

    13:

    //

     

    Convert

     

    other

     

    types

     

    to

     

    String

     

    by

     

    interpolation

     

    (

    via

     

    $

     

    sign

    )

     

    14:

    String

     

    sample

     

    =

     

    $

    counter

     

    $

    isVisible

    ;

     

    15:

    //

     

    Extend

     

    functionality

     

    of

     

    double

    ’-

    type

     

    (

    except

     

    null

     

    -

     

    a

     

    keyword

    )

     

    16:

    extension

     

    DoubleExt

     

    on

     

    double

     

    {

     

    17:

     

    //

     

    0.12.

    pcnt

     

    =>

     

    ’12.0%’

     

    18:

     

    String

     

    get

     

    pcnt

     

    =>

     

    $

    {

    this

     

    *

     

    100}%

    ;

     

    19:

    }

    Collections are represented by ‘List‘, ‘Map‘, ‘Set‘, ‘Records‘, and ‘Queue‘. Where ‘List‘ is an ordered collection of objects; ‘Map‘, as a collection of key/value pairs, is used to retrieve a value by its associated key with a maintained key uniqueness; as well as ‘Set‘ – to control uniqueness of variables. ‘Queue‘ implements both stack and queue behavior (where, ListQueue – keeps a cyclic buffer of elements, DoubleLinkedQueue – to guarantee constant time on ’add’, ’remove-at-ends’ and ’peek’-operations). As a distinctive feature of collections, their iterativeness is declared by a subtype:

    ‘HashMap‘ and ‘HashSet‘, unordered,both provide an access to items (by key)in (potentially) amortized constant time;

    ‘LinkedHashMap‘ and‘LinkedHashSet‘ iterate in key insertionorder;

    ‘SplayTreeMap‘ and ‘SplayTreeSet‘are based on a self-balancing binary treethat allows most single-entry operationsin amortized logarithmic time;

    01:

    List

    <

    int

    >

     

    data

     

    =

     

    [1,

     

    2,

     

    3];

     

    //

     

    data

    .

    last

    ;

     

    //

     

    3

     

    02:

    Set

    <

    String

    >

     

    scope

     

    =

     

    {

    a

    ,

     

    b

    ,

     

    a

    };

     

    //

     

    scope

    .

    length

    ;

     

    //

     

    2

     

    03:

    HashMap

    <

    String

    ,

     

    int

    >

     

    hash

     

    =

     

    {

    test

    :

     

    123};

     

    //

     

    hash

    .

    values

    ;

     

    //

     

    [123]

     

    04:

    //

     

    Records

     

    -

     

    immutable

    ,

     

    not

     

    iterable

    ,

     

    with

     

    an

     

    object

    -

    based

     

    access

     

    05:

    var

     

    map

     

    =

     

    (

    text

    :

     

    sample

    ,

     

    at

    :

     

    123)

    ;

     

    //

     

    map

    .

    text

    ;

     

    //

     

    sample

    Extracting Patterns

    Pattern matching in programming involves comparing data structures against predefined patterns, which enables conditional execution based on the match:

    01:

    const

     

    a

     

    =

     

    a

    ;

     

    02:

    const

     

    b

     

    =

     

    b

    ;

     

    03:

    final

     

    obj

     

    =

     

    [

    a

    ,

     

    b

    ];

     

    04:

     

    05:

    if

     

    (

    obj

    .

    length

     

    ==

     

    2

     

    &&

     

    obj

    [0]

     

    ==

     

    a

     

    &&

     

    obj

    [1]

     

    ==

     

    b

    )

     

    {

     

    06:

     

    print

    (

    $

    a

    ,

     

    $

    b

    )

    ;

     

    07:

    }

     

    08:

    //

     

    Or

    ,

     

    it

     

    can

     

    be

     

    transformed

     

    into

    :

     

    09:

    switch

     

    (

    obj

    )

     

    {

     

    10:

     

    case

     

    [

    a

    ,

     

    b

    ]:

     

    11:

     

     

    print

    (

    $

    a

    ,

     

    $

    b

    )

    ;

     

    12:

     

     

    break

    ;

     

    13:

     

    //

     

    ...

     

    other

     

    conditions

     

    14:

    }

    Destructuring, on the other hand, offers a means to extract individual elements from complex data structures like arrays or objects and assign them to variables. This technique is especially valuable when dealing with complex objects, allowing you to fetch their properties efficiently:

    01:

    //

     

    Operation

     

    with

     

    basics

     

    02:

    final

     

    users

     

    =

     

    [

    User

     

    1

    ,

     

    User

     

    2

    ,

     

    User

     

    3

    ];

     

    03:

    final

     

    [

    adminName

    ,

     

    userName

    ,

     

    guestName

    ]

     

    =

     

    users

    ;

     

    04:

     

    05:

    //

     

    To

     

    declare

     

    new

     

    variables

     

    from

     

    a

     

    nested

     

    structure

     

    06:

    var

     

    (

    name

    ,

     

    [

    code

    ,

     

    type

    ])

     

    =

     

    (

    User

     

    1

    ,

     

    [123,

     

    true

    ])

    ;

     

    07:

     

    08:

    //

     

    Object

     

    extraction

     

    09:

    class

     

    Transaction

     

    {

     

    10:

     

    String

     

    description

    ;

     

    11:

     

    String

     

    category

    ;

     

    12:

     

    double

     

    amount

    ;

     

    13:

    }

     

    14:

     

    15:

    const

     

    scope

     

    =

     

    [

     

    16:

     

    Transaction

    (

    Buy

     

    groceries

    ,

     

    Groceries

    ,

     

    50.0)

    ,

     

    17:

     

    //

     

    ...

     

    others

     

    18:

    ];

     

    19:

     

    20:

    for

     

    (

    final

     

    Transaction

    (:

    category

    ,

     

    :

    amount

    )

     

    in

     

    scope

    )

     

    {

     

    21:

     

    print

    (

    $

    category

    :

     

    $

    amount

    )

    ;

     

    22:

    }

    Operating with Dates

    Let’s revise a few build-in capabilities of dates’ type from an example:

    - IF ‘end‘-date is lower or equal to ‘current‘-date THEN return 1

    - IF ‘start‘-date is equal to ‘current‘-date THEN return 0

    - ELSE return a relative value in the range of ‘0...1‘ by calculating the float value based on the ratio of the number of days between ‘start‘ and ‘current‘ to the total number of days between ‘start‘ and ‘end‘.

    01:

    calculateValue

    (

    DateTime

     

    start

    ,

     

    DateTime

     

    end

    ,

     

    DateTime

     

    current

    )

     

    {

     

    02:

     

    if

     

    (

    end

    .

    isBefore

    (

    current

    )

     

     

    end

    .

    isAtSameMomentAs

    (

    current

    )

    )

     

    03:

     

     

    return

     

    1;

     

    04:

     

    if

     

    (

    start

    .

    isAtSameMomentAs

    (

    current

    )

    )

     

    05:

     

     

    return

     

    0;

     

    06:

     

    int

     

    total

     

    =

     

    end

    .

    difference

    (

    start

    )

    .

    inDays

    ;

     

    07:

     

    int

     

    diff

     

    =

     

    current

    .

    difference

    (

    start

    )

    .

    inDays

    ;

     

    08:

     

    return

     

    diff

     

    /

     

    total

    ;

     

    09:

    }

    ‘DateFormat‘ is used to convert and parse dates into a specific format, such as ’yyyy-MM-dd’ (4 characters for a year, dash, 2 symbols for a month, dash, last two to identify the date), while considering localization preferences.

    01:

    DateTime

     

    dt

     

    =

     

    DateTime

    (2023,

     

    11,

     

    1)

    ;

     

    02:

    var

     

    text

     

    =

     

    DateFormat

    .

    yMMMEd

    ()

    .

    format

    (

    dt

    )

    ;

     

    03:

    print

    (

    text

    )

    ;

     

    //

     

    Wed

    ,

     

    Nov

     

    1,

     

    2023

     

    04:

    //

     

    Parsing

     

    stringified

     

    date

     

    back

     

    without

     

    exceptions

     

    on

     

    failure

     

    05:

    DateTime

    ?

     

    value

     

    =

     

    DateTime

    .

    tryParse

    (

    text

    )

    ;

    And, we may extend ‘DateTime‘ by an insensible usage of ‘DateFormat‘-class:

    01:

    extension

     

    DateTimeExt

     

    on

     

    DateTime

     

    {

     

    02:

     

    //

     

    DateTime

    (/*

     

    ...

     

    */)

    .

    toMonthDay

    ()

    ;

     

    03:

     

    String

     

    toMonthDay

    (

    Locale

    ?

     

    locale

    )

     

    =>

     

    04:

     

     

     

    DateFormat

    .

    MMMMd

    (

    locale

     

    ??

     

    en_US

    )

    .

    format

    (

    this

    )

    ;

     

    05:

    }

    ‘DateTime‘-class doesn’t inherently handle time zones, but it can be covered by external libraries like ‘timezone‘ or ‘intl‘. To facilitate time processing, one can utilize the Unix timestamp, also known as an epoch timestamp, through the properties ‘microsecondsSinceEpoch‘ and ‘millisecondsSinceEpoch‘.

    Additionally, for application testing purposes, consider the use of libraries like ‘fake_datetime‘ to mock the current time, enabling more effective testing scenarios.

    Understanding Operations

    Dart provides a wide range of built-in functions and libraries to cover various operations and functionalities:

    01:

    double

     

    num1

     

    =

     

    10.0;

     

    double

     

    num2

     

    =

     

    3.0;

     

    double

    ?

     

    num3

    ;

     

    02:

    var

     

    result

     

    =

     

    num1

     

    +

     

    num2

    ;

     

    //

     

    Addition

    :

     

    13.0

     

    03:

    result

     

    =

     

    num1

     

    -

     

    num2

    ;

     

    //

     

    Subtraction

    :

     

    7.0

     

    04:

    result

     

    =

     

    num1

     

    *

     

    num2

    ;

     

    //

     

    Multiplication

    :

     

    30.0

     

    05:

    result

     

    =

     

    num1

     

    /

     

    num2

    ;

     

    //

     

    Division

    :

     

    3.3333333333333335

     

    06:

    //

     

    ’~/’

     

    Integer

     

    Division

     

    (

    Floor

     

    Division

    )

     

    07:

    result

     

    =

     

    num1

     

    ~/

     

    num2

    ;

     

    //

     

    3

     

    (

    int

    )

     

    08:

    //

     

    ’%’

     

    Modulus

     

    (

    Remainder

    )

     

    09:

    result

     

    =

     

    num1

     

    %

     

    num2

    ;

     

    //

     

    1.0

     

    10:

    //

     

    ’??’

     

    null

    -

    check

     

    statement

     

    11:

    num3

     

    ??

     

    num1

    ;

     

    //

     

    return

     

    num1

     

    if

     

    num3

     

    is

     

    null

     

    12:

    num3

     

    =

     

    2.0;

     

    13:

    //

     

    sqrt

     

    Square

     

    Root

     

    14:

    result

     

    =

     

    sqrt

    (

    num3

    )

    ;

     

    //

     

    1.4142135623730951

     

    15:

    //

     

    pow

     

    Exponentiation

     

    16:

    result

     

    =

     

    pow

    (

    num3

    ,

     

    3)

    ;

     

    //

     

    8.0

     

    17:

    //

     

    sin

     

    Sine

     

    18:

    result

     

    =

     

    sin

    (

    num3

    )

    ;

     

    //

     

    0.9092974268256817

     

    19:

    //

     

    cos

     

    Cosine

     

    20:

    result

     

    =

     

    cos

    (

    num3

    )

    ;

     

    //

     

    -0.4161468365471424

     

    21:

    //

     

    tan

     

    Tangent

     

    22:

    result

     

    =

     

    tan

    (

    num3

    )

    ;

     

    //

     

    -2.185039863261519

     

    23:

    //

     

    log

     

    Natural

     

    Logarithm

     

    24:

    result

     

    =

     

    log

    (

    num3

    )

    ;

     

    //

     

    0.6931471805599453

     

    25:

    //

     

    log10

     

    Base

    -10

     

    Logarithm

     

    26:

    result

     

    =

     

    log

    (

    num3

    )

     

    /

     

    ln10

    ;

     

    //

     

    0.3010299956639812

     

    27:

    //

    /

     

    Overloading

    :

     

    null

     

    +

     

    5

     

    =

     

    5

     

    28:

    extension

     

    Ext

    <

    T

     

    extends

     

    num

    >

     

    on

     

    T

    ?

     

    {

     

    29:

     

    T

    ?

     

    operator

     

    +(

    T

     

    val

    )

     

    =>

     

    this

     

    !=

     

    null

     

    ?

     

    this

     

    +

     

    val

     

    :

     

    val

    ;

     

    30:

    }

     

    31:

    //

    /

     

    Overloading

     

    ’ ’

     

    to

     

    merge

     

    maps

     

    32:

    extension

     

    Merge

    <

    T

    ,

     

    K

    >

     

    on

     

    Map

    <

    T

    ,

     

    K

    >

     

    {

     

    33:

     

    Map

    <

    T

    ,

     

    K

    >

     

    operator

     

    (

    Map

    <

    T

    ,

     

    K

    >

     

    other

    )

     

    =>

     

    34:

     

     

     

    {...

    this

    }..

    addEntries

    (

    other

    .

    entries

    )

    ;

     

    35:

    }

    In addition, each type has own operands:

    01:

    //

     

    Range

     

    limitation

    :

     

    ’-10.0’

     

    to

     

    ’0.0’;

     

    ’100.0’

     

    to

     

    ’3.0’

     

    02:

    return

     

    (

    num

     

    as

     

    double

    )

    .

    clamp

    (0,

     

    3)

    ;

     

    03:

    //

     

    Dates

     

    comparison

     

    04:

    DateTime

    (

    now

    .

    year

    ,

     

    now

    .

    month

    )

    .

    isAfter

    (

    createdAt

    )

    ;

     

    05:

    createdAt

    .

    isBefore

    (

    DateTime

    .

    now

    ()

    )

    ;

     

    06:

    //

     

    Transformations

     

    07:

    final

     

    m

     

    =

     

    {

    Sample

    :

     

    1,

     

    sample

    :

     

    2};

     

    08:

    CanonicalizedMap

    .

    from

    (

    m

    ,

     

    (

    k

    )

     

    =>

     

    k

    .

    toLowerCase

    ()

    )

     

    //

     

    {’

    Sample

    ’:

     

    1}

    Overloading Operators

    Magic methods are often referred to as operator overloading or special methods. They allow to declare a custom behavior for built-in operations:

    ‘toString‘ returns astring representation of an object, can beused for a serialization and deserializationprocess of an object;

    ‘call‘ allows an object to be treated asa function;

    ‘hashCode‘ returns a hash code for anobject (to use it as a key for ‘Map‘ and‘Set‘, and to override ‘==‘);

    ‘operator‘: overload basic operands (as‘==‘ to compare, ‘+‘ to sum, etc.);

    ‘get‘ and ‘set‘ – to override the behaviorof getting and setting properties.

    01:

    class

     

    Person

     

    {

     

    02:

     

    //

     

    Private

     

    property

     

    -

     

    null

     

    or

     

    DateTime

     

    03:

     

    DateTime

    ?

     

    _createdAt

    ;

     

    04:

     

    //

     

    Required

     

    from

     

    a

     

    constructor

     

    since

     

    cannot

     

    be

     

    null

     

    05:

     

    String

     

    name

    ;

     

    06:

     

    //

     

    Post

    -

    initialization

     

    07:

     

    late

     

    DateTime

     

    _createdAt

     

    =

     

    DateTime

    .

    now

    ()

    ;

     

    08:

     

    //

     

    var

     

    person

     

    =

     

    Person

    (’

    Tom

    ’)

    ;

     

    09:

     

    Person

    (

    this

    .

    name

    )

    ;

     

    10:

     

    //

     

    person

    ()

     

    //

     

    Hello

     

    from

     

    Tom

    !’

     

    11:

     

    String

     

    call

    ()

     

    =>

     

    Hello

     

    from

     

    $

    name

    !

    ;

     

    12:

     

    //

     

    person

    .

    createdAt

     

    =

     

    DateTime

    (2023,

     

    01,

     

    01)

    ;

     

    13:

     

    set

     

    createdAt

    (

    DateTime

     

    date

    )

     

    =>

     

    _createdAt

     

    =

     

    date

    ;

     

    14:

     

    //

     

    print

    (

    person

    .

    createdAt

    )

    ;

     

    //

     

    2023-01-01

     

    00:00:00

     

    15:

     

    DateTime

     

    get

     

    createdAt

     

    =>

     

    _createdAt

    ;

     

    16:

     

    //

     

    print

    (

    Person

    (’

    Tom

    ’)

     

    ==

     

    Person

    (’

    Terry

    ’)

    )

    ;

     

    //

     

    false

     

    17:

     

    @override

     

    //

     

    Pre

    -

    requisite

     

    for

     

    any

     

    operator

     

    change

     

    18:

     

    int

     

    get

     

    hashCode

     

    =>

     

    hashValues

    (

    name

    )

    ;

     

    //

     

    core

    -

    method

     

    to

     

    hash

     

    value

     

    19:

     

    @override

     

    //

     

    covariant

     

    limits

     

    comparison

     

    to

     

    the

     

    same

     

    class

     

    20:

     

    bool

     

    operator

     

    ==(

    covariant

     

    Person

     

    other

    )

     

    =>

     

    other

    .

    name

     

    ==

     

    name

    ;

     

    21:

     

    //

     

    person

     

    =

     

    Person

    .

    fromString

    (’

    Tom

    ’)

    ;

     

    22:

     

    factory

     

    Person

    .

    fromString

    (

    String

     

    name

    )

     

    {

     

    23:

     

     

    return

     

    Person

    (

    name

    )

    ;

     

    24:

     

    }

     

    25:

     

    //

     

    print

    (

    person

    )

    ;

     

    //

     

    Tom

     

    26:

     

    @override

     

    27:

     

    String

     

    toString

    ()

     

    =>

     

    name

    ;

     

    28:

    }

    Declaring Input Arguments

    A few options are available for the arguments declaration:

    01:

    //

     

    Ordered

     

    scope

     

    02:

    //

    /

     

    Sample

    :

     

    add

    (’

    test

    ’,

     

    null

    )

     

    03:

    //

    /

     

    Sample

    :

     

    add

    (’

    test

    ’,

     

    123)

     

    04:

    void

     

    add

    (

    String

     

    value

    ,

     

    int

    ?

     

    id

    )

    ;

     

    05:

     

    06:

    //

     

    With

     

    optional

     

    arguments

     

    07:

    //

    /

     

    Sample

    :

     

    add

    (’

    test

    ’)

     

    08:

    void

     

    add

    (

    String

     

    value

    ,

     

    [

    int

    ?

     

    id

    ])

    ;

     

    09:

    void

     

    add

    (

    String

     

    value

    ,

     

    [

    int

     

    id

     

    =

     

    123])

    ;

     

    //

     

    preset

     

    for

     

    id

     

    10:

     

    11:

    //

     

    Named

     

    attributes

     

    12:

    //

    /

     

    Sample

    :

     

    add

    (

    value

    :

     

    test

    ’)

     

    13:

    //

    /

     

    Sample

    :

     

    add

    (

    id

    :

     

    1,

     

    value

    :

     

    test

    ’)

     

    14:

    void

     

    add

    ({

    String

     

    value

    ,

     

    int

    ?

     

    id

    })

    ;

     

    15:

    void

     

    add

    ({

    String

     

    value

    ,

     

    int

     

    id

     

    =

     

    123})

    ;

     

    16:

     

    17:

    //

     

    Mix

     

    18:

    //

    /

    Enjoying the preview?
    Page 1 of 1