A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven

in utopian-io •  4 days ago

dart-bird.jpg

Repository

https://github.com/dart-lang/sdk

What Will I Learn?

  • You will learn to use Generic Types
  • You will learn about Exceptions and how to make custom Exceptions
  • You will learn about factory constructors
  • You will learn how to use Factory constructors to build singleton objects
  • You will learn about Enumerated Types

Requirements

System Requirements:
OS Support for Dart:
  • Windows
  • macOS
  • Linux

Required Knowledge

  • The Dart SDK
  • A Dart supported text editor (Dart Pad can be used)
  • A little time to sit and watch a video and some patience to learn the language

Resources for Dart:

Sources:

Dart Logo (Google): https://www.dartlang.org/

Difficulty

  • Beginner

Description

In this Dart Tutorial Video, we look at Generic Types, Exceptions, Factory Constructors, Enumerated Types and Singleton Objects. While many of these ideas are more complex than any of the concepts that we've looked at before, they can be very powerful. Generic Types and Enumerated Types help build out the type system and Exceptions help us deal with different types of errors. Factory constructors are important because they allow us to build Single instance classes or Singleton Objects.

Generic and Enumerated Types

The Idea of Generic Types is an idea that has been around for awhile and it is usually found it functional statically typed languages. Generic Types allow the developer to build out Polymorphic objects, functions and variables which are targeted to specific patterns. This enables us to create functions and classes that can be reused with various different types and it cuts down on the boilerplate that needs to be built for an application. Enumerated Types on the other hand give us the ability to deal with binary types. We can again created targeted situations with Enumerated types that narrow down how our application state can be derived.


enums-generics.png

In the above image, we have a Status enum type and a function that uses two generic types T and V. The Status enum type can be only one of three different states, Status.running, Status.stopped or Status.paused. Each of these type has its own index representation as well with the first type being index 0 by default. The generic function add takes in two types T and V with both types extending the num type. This means that these types can both be independent types that must also extend the num type. This narrows down how the function must be used in our code and will also decrease how the virtual machine unpacks this code.

Factory Constructors and Singleton Objects

Factory Constructors are an idea that came from the Factory pattern. The main idea behind this pattern was to create a method that doesn't have to specify the exact object that will be created by the constructor. Factories can be used to create sub classes and singleton objects as a result and you can use them to defer the instantiation of a class. They were added to the Dart specification because the pattern was very common in other Object Oriented Languages prior to the creation of the language.


singleton.png

The class above features and example of a simple singleton object class. This class Item contains a static field item which holds a reference to the singleton object after it is instantiated. The Factory constructor for this class takes in the initial state and determines whether or not that item field contains a object or not inside of it. If it does contain an object, then the factory constructor just returns that object with the new added state appended to it. If it doesn't contain an object then it creates a new Item object using the Item._internal() named constructor. In this way, we can call to this Item constructor as many times as we want, but only have one single Item object in the entire program.

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-6

Video Tutorial

Curriculum

Projects

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi @tensor

Thank you for your contribution to the video category.

Again, this is a fantastic tutorial to add to the Beginners guide to Dart. It is great to see you developing this series with new concepts added each time. I like the way you define each types with clarity in the video.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

·

Thank you @rosatravels as always.

Say as a noob in programming/coding, what basic programming language would you advise to learn before starting this tutorial?

·

This entire series is made for people like you. I recommend you go to the beginning and just start there.

·
·

Thanks alot was having fears of not having basic programing knowledge.

·
·
·

Not a problem. If you find you have anymore questions feel free to ask either on YouTube or steem.

Hi, @tensor!

You just got a 5.82% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!