Catch Exceptions in Flutter Like Never Before!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is how you handle exceptions in your application but this is not an efficient way of handling those errors so in this video I'm going to list out all the possible approaches you could have taken and the problems with them and finally explain the efficient solution which counters all of those problems so what is wrong with this function this is an asynchronous function which returns user and the HTTP call is wrapped in try and catch block then a request is sent to the server which responds to us with the status code if it's 200 which means it's okay then it Returns the user model otherwise it just throws some unexpected error this row message is received in the sketch block which again throws the exception that's received this function is then called in the UI file where we want to display the username and when we restart the application we get unhandled exception why do we get this the reason for that is we have thrown the exception from this get request to wherever this function is called so to resolve this error we simply need to wrap this call in the try and catch block take all of this above above and put it in the try block and in the catch block we'll again set State and set text equal to whatever error is there after again restarting the application we don't get unhandled exception instead the error shows up on the device screen imagine doing this for hundreds and thousands of functions it's quite easy to forget to put try and catch block sometimes it's not even necessary because the exception is a handle layer itself the root of this problem is this part right here the function signature the function signature doesn't specify if we need to handle error or not so to fix that we can use the Tuple package and then here add Tuple to to Bill allows us to store multiple data types or multiple values in a single variable since our function cannot return two values we will use two pill to encapsulate both of those values so here we just need to pass in the two data types that we return from this function first is a string because in case of failure we will pass in a string and in case of success we have user now we just need to enclose this in the Tuple 2 data type the first item is a string and we need to display a string only if there is an error so this can be of the type string nullable because we don't have an error when the status code is 200 so here we can pass in null and the item 2 which is the user model can be this user Dot from Json in the catch block though we will have returned Tuple 2 with the first item being e.2 string because that's the error we want to display and item 2 will be null so we'll also make user a nullable return data type then we can save it the reason we have not mentioned return tupel 2 over here is because if we throw this it will ultimately come in this catch block and this catch block will just display that as it is in our UI file we will check first if the result dot item1 item 1 stands for the failure part which means a string so if it is not equal to null it means it's a failure because string is not null so here we will do set State and put text as rest dot item1 which is not nullable if item 1 is not null then we will set State and put text as rest dot item2 dot name then we can remove the try and catch block which is not necessary anymore and when we restart the application we don't get any exception and here the error shows up as it is the problem with this approach is again imagining if there are thousands of functions it's very inconvenient to write is not equal to null for every item that's there and it's also quite easy to forget it third approach can be to create a new file then so we'll just create error handle dot dot file and here create a class called error handle this will contain two values error and success this is dynamic instead of a user model the reason for that is this class is going to be reusable so it's not just going to fetch data from the user if you're developing an e-commerce website this can also be a product model or a rating model it can be anything thing that's right it's Dynamic now we can just create a Constructor for these fields and in the error controller instead of returning a tuple you can just return error handle and here we'll just return error handle Force the error message this is going to be null so even in the error handle we are going to have string nullable and success will be user model from Json response dot body and here we are going to return error handle error as e.2 string and success as null in main.dot file now instead of having response dot item1 we'll have response dot error and this will be response dot error otherwise we'll just take success and it doesn't identify the name property because it's Dynamic so you can use as user which then gives us access to the name property then we can restart the app we don't get any exceptions and the error message shows up this approach has two problems first is the use of dynamic I would always recommend to not use Dynamic for any data type unless it's really necessary and the second problem is again having those if conditions it's quite easy to forget and in fact it's a lot of code to write as well so what is the efficient solution for this to avoid this pain you can use FP dot or dot Z package which is available on pub.dev after install calling it all you need to do is mention here the data type which can be either so this can either be a string or a user and we don't have to mention it as nullable this basically means we are either going to return a string or a user from this function so in case of success we are going to return right we need to return user so if you even type to mention a string it will give you an error so we just need to pass in user Dot from jsonresponse.body but in case of error we just need to return left left stands for failure and here if you try to pass in a user model you will get an error because here we need to pass in the string whatever is mentioned over here this will just be e.2 string to resolve the issues we can go to the UI file and this is not needed anymore all you need to do is check the data type of response which is either string or user and having either in the return data type will provide us with a method called fold which fold we can handle what to do in case of failure which is contained in L and what to do in case of success which is contained in the variable R so in case of L I just want text to be equal to l and in case of r i want text equal to r dot name and just searched it so that the build function rebuilds then we can restart the app we don't get any error this counters all of our problems we've had it mentions the data type so we know we need to handle this exception it provides us with methods to handle both of these errors which is pretty quick for us but the only problem we'll have is continuously writing this part again and again so if we use this line for like thousands of functions which we will and then we realize we just want to change class of this one variable which is the failure part because we can replace it with our custom class which is what is usually done in case of displaying failures because it's not just an error message it can be the stack trace and multiple other things as well so what we can do is create type definition over here which can be called either user which will be equal to what whatever we are returning over here now we can just take this type definition and put it over here and if you hover over this the function signature Remains the Same but if we want to change something for example just swap the success and the failure class with each other you can see this reflects and now we can make those changes necessary so in case of success we will return a string and in case of failure we are going to return a user model and here respectively change those values but you'll say this approach is only valid when we won't want to have success as a user model but no you can make slight changes and this can change for example here at the generic type T and here mention it as T so now whenever you use either user you can mention the type we will pass in user and this works successfully whatever we pass in the generics it will just assign it to the success class so if I just want the success class to be a string as well I can have that and here when returning right I just want a string to be present there's much much more you can do with the FP dot package I'm just going to show you some of the functions you can have and multiple more resources will be linked in the description below check them out so in the May in the UI file if you just want to handle the success part you can do response dot fold right and it will allow you to handle the success in case of handling only the failure you can just do fold left if you directly want to get the value without folding you can do get left and get right if you want to check if it's a failure then you can call is left otherwise you can just call is right and if you want to swap the values like we did previously you can call the swap method so this is it for the tutorial if you want to know more about such efficient techniques or guides on a certain topic or project make sure to subscribe the channel thank you for watching see you in the next tutorial
Info
Channel: Rivaan Ranawat
Views: 17,424
Rating: undefined out of 5
Keywords: flutter, flutter tips, flutter tricks, flutter exceptions, dart, try catch, flutter efficient, flutter tips tricks, flutter efficient catching, flutter efficient exception handling, dart exceptions, tutorial, flutter 3, flutter 3 tutorial, dartz, fpdart
Id: noNs58-CmSE
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Thu Oct 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.