Pattern Matching in Dart 3 is Powerful!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we now have pattern matching in Dart 3. let's take a look at what it is and my favorite cases of when we can use it first off let's just take a look at the documentation here we have the documentation for pattern matching in general you can find this on the language tour of tart.dev for patterns here you can see and read everything about pattern matching some use cases examples and all of that you can also find pattern types here you can see everything you can kind of match patterns to so for example we can match to constant we can match to literals whatever everything else that we have here but let's take a look at two of my favorite cases of using them so now we're going to kind of simulate a state variable I'm going to make it Global and we're going to check yet the old implementation code for how we can check if a value is not null and then execute a method so let's just create in nullable value called name and just give it the name John we're going to create a new method just do something and we're going to give it a required or a a variable which is just the string name you can see that this is non-nullable we're just going to print it so for example if we would do something like do something here we would say see that okay this requires a non-nullable type because our name is nullable and the required variable or the required parameter is um non-nullable so we would probably do something like if name is not equal to null the type system is still telling us that this name variable is nullable the reason for this is because the global variable of name could be changed anywhere so we are not sure if it's changed during the time it has gone through the if Case to then do the execution of the do something method so something we do before would create a local variable because then we could make sure that this variable is not being changed so for example you can do final underscore name is equal to name and instead of using the um the global variable because instead use um this variable Let's ignore that this is like underscore name let's say your name my name I don't know and we can see that yeah this is working as expected with pattern matching we can actually do another cool thing so let's actually remove all of these we're going to do the same thing but we're not going to create our kind of local variable above the if case so we're just going to start creating an if case so we can do name because that's the thing we want to shake we can call case to make sure that we're doing a pattern match and we can do final name question mark so what this means is that we're doing we're creating a new variable here but we're using pattern matching to make sure that we only want to match patterns that are non-nullable and if we use a question mark this just means that we're going to com continue into the if case if it's non-nullable if we do a exclamation mark we would actually use fail in this case you can read this in so for example you can see the null check we have sub patterns and you can go read all about this as well but in this case we have our new variable so now we can do actually do something and we can pass name in this case it will actually be non-nullable so we won't have to define something new right we can just do this if case and then we're fine that's the first example of something I really like using the new pattern matching syntax we're going to look at one of my other favorite ones so let's actually use remove all of this we're going to first actually create a enum we're just going to call it access type we're going to have admin user and then denied I guess um so now you can imagine that we would have two variables this is an example but you might see this actually in your code let's define two variables so we have a Bool for has access let's imagine that that's true but we also have a pool for this owner or something like that which is false in this case actually let's define a method so it's going to return a access type there we go we're going to just call this method so now we have two variables so what we would probably do before is something like this where you would first Shake an if case or have access then you would check an owner if you're an owner you return admin and then so on in this case we have constant values that's why it's pretty much saying that some of the values are good code what we can do now is actually use a switch case with pattern matching and records together so we can do something like return switch and we want to check both of these values together in different combinations so what we can do is add two new or like add to new parentheses to make sure that we're creating a record the first value is going to be has access the second value is going to be has or its owner so in here we can start shaking the different types so for example here we have true true where the first true is for the has access the second true is for the is owner in the second case we have two four has access false phase owners we return the access type of user and the last one we can actually use ignore all cases so in this case it will be denied um and we can actually use double check all of this so now we have true for hex has access and then false for each owner we can just run this we can see that we have now the access type of user we can do so let's imagine that each owner is true as well we have the access type of admin we can make both of them false and we have access type of denied and as you can see what we have done here is using a switch expression we have used records to create a new kind of variable in this case or a record and we can start like switching on top of this to then just return the different types here if you liked the video make sure to like And subscribe I also want you to share your favorite use case of pattern matching down in the comments and you can probably check the recommended video here somewhere I would assume at least hopefully maybe it's good at least yeah
Info
Channel: Robert Brunhage
Views: 4,585
Rating: undefined out of 5
Keywords: Flutter, Dart, Dart 3, Pattern Matching, Dart 3 records, dart 3 pattern matching, Dart 3 destructuring, Flutter 3.10, flutter pattern matching, flutter records
Id: j3fzeDpd2ts
Channel Id: undefined
Length: 7min 14sec (434 seconds)
Published: Sat May 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.