Dart Crash Course #3 - Type Annotations
Key Takeaways
The video covers the basics of type annotations in Dart, including explicit type annotations, null safety, and non-nullable variables by default. It demonstrates how to use type annotations with variables, including strings, integers, booleans, and doubles.
Full Transcript
all right then gang so now we've seen how to create variables using three different keywords VAR final and const we've also seen that once we've given a variable a type of data that type of data can then be changed so for example this name variable is a string and therefore it can only ever be a string in the future now so far we've not explicitly added any type annotations to the variables to say what type they should be instead we've just given them a value and that infers that type based on the value so in this name example we've not explicitly said anywhere that this name has to be a string we've just given that variable a string value and D infers that type of the variable to be a string meaning it always then must be a string in the future however we can also explicitly add type annotations to variables to say what type of data they should hold for example I could replace this vary word with string with a capital S to say this variable called name must be a string so we're adding this explicit it type annotation now to the variable and if we try to assign a different value which wasn't a string to the variable then we're going to get an error saying that we can't do that because we've explicitly added the string type annotation to the variable to say whatever value is inside this name variable must be a string so we can also do the same with other types of data as well so for example let me just come down below this print statement I'm going to create an integer now so normally I would say V AG is equal to something like 2 five but instead of v i could say int like so and now we're saying this is an integer this age thing so if I tried to assign a different value like a Boolean then we're going to get an error because this is not an integer so let me get rid of that and change it back to 25 and also we get this yellow underline because we're not using the variable so we'll print it out to get rid of that print age okay so we can also use booleans and we can type those by saying Bo and then we'll say is open open I we'll set that equal to true and now this must be a Boolean so if I tried to change this to a string then it's not going to let me do that we get an error because this is not a Boolean it must be a Boolean true or false so let's print that again so prints down here is open to get rid of that little underline and also we can use doubles so this is a number right 25 but it's an integer a double would be something that has a decimal point so for example 25.5 so I could say double and we'll call this average rating and we'll set this equal to 7.9 for example so that would be a double right here and if we hover over that there's no error but if we change this to a string for example then we should get an error because this is not a double now if we make this an integer like so then we don't actually get an error so if we try printing this out now let me say print and then average rating I'm going to run this code over here so hopefully we're going to see all of these things printed to the right you can see it works so we can say double and use what is essentially an integer here but I couldn't add a0 five on here I would get an error because this is a double right but in the future if we changed the Aver aage rating we can do it to something like 7.9 because this is a double and we're allowed decimal points on doubles okay so if we run this again then we should see 7.9 down here awesome so I just wanted to make clear as well when we're using these types it's only the type that can't change so we can't change the value I could say name is equal to Luigi that would be absolutely fine I just can't change the name type into something else likewise for the age we can change that to 30 and we'll do this as well we'll say is open is false like so and that's all absolutely fine if we run this we should get the updated values printed over here to the console again it's just the type of data that can't change now if you wanted something to be either final or const you can just put that before the type annotation so I could say now if I wanted to const string name equals Mario and now notice we can't actually change the value because we've declared this as a constant so let me get rid of that I could do the same thing down here I could say final this time and again I would get an error right here we are getting this blue underline because it's saying look if you know this at compile time why don't you change this into a constant that's fine I just wanted to show you we could do this with final as well but we'll change it to constant to stop the complaining and we get an eror down here where we try to update the value of age because we can't do that now all right so this is how we use these explicit type annotations to say what these variable types should be okay so there are other data types in Dart as well like lists sets and Maps which we're going to talk about later in the course but the same rules would apply to those types as well we could add the type annotations manually before the variable name or we could leave dat to infer the type for us now a lot of the time I probably won't be adding type annotations in front of variables and just let D in further types instead but there will be some occasions where I do manually add in the type annotation instead and one of those occasions might be where we want to declare a variable and specify what type it should be but not necessarily give it a value right away for example I might have a variable called points which must be an integer now I might not want to give this points variable a value to begin with but instead just update it later on in the code for now I just want to declare the variable and say it must be of type int now there's a small problem with this code because when we use type annotations to declare a variable the variable cannot be null before it gets used it's non-nullable by default and that means if I try to print out the points variable then I'm going to get an error saying that the variable is non-nullable and it needs to be assigned a value before it gets used in the code this behavior in D is called null safety and it's there to prevent unintentionally having access to variables which which are null however sometimes you might want a variable to be null before it gets initialized with an actual value and to allow that null value for a variable created this way we just need to add a question mark directly after the type annotation now we're saying that this variable must be an INT when we assign a value to it but until then it's okay being null and we now no longer get an error all right so hopefully now you understand a little bit about how we can add type annotations to variables we will also be adding type annotations to other things in the rest of this course as well starting with functions in the next lesson
Original Description
In this Dart crash course, you'll learn how to code in Dart from the ground up, in prep for making applications with Flutter (which uses Dart).
🚀🥷🏼 Get early access to this entire course now on Net Ninja Pro:
https://netninja.dev/p/dart-crash-course
💻🥷🏼 TypeScript Masterclass:
https://netninja.dev/p/typescript-masterclass
💻🥷🏼 TypeScript Crash Course:
https://www.youtube.com/watch?v=VGu1vDAWNTg&list=PL4cUxeGkcC9gNhFQgS4edYLqP7LkZcFMN
💻🥷🏼 Modern JavaScript Course:
https://netninja.dev/p/modern-javascript-from-novice-to-ninja
🔗🥷🏼 Dartpad - https://dartpad.dev/
🔗🥷🏼 Dart docs - https://dart.dev/guides
🔗🥷🏼 Pub.dev - https://pub.dev/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI