Flutter Tutorial for Beginners #29 - Error Handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay no my friends so currently our code all seems to be working correctly but what if at some point there's an error somewhere along the line when we tried to collect the data for example when we instantiate the class we might pass in a URL for the endpoint over here that's not valid or it could be that this is not valid over here so we could add say an S on here and this is not going to be the correct endpoint anymore and therefore when we try to get data from the endpoint it's not going to return towards the response that we expect and then when we tried to convert that response into a map that will probably fail therefore all of this is going to fail at some point and when we go to set the time property of this instance then it's not going to be what we expect and so when we try to update that time and output it then it's not going to work so let's just demo this I've changed this so it shouldn't work now I'm gonna save it and then I'm going to go to run and press hot restart now notice over here we're going to see in a second loading but that never changes because we never get a valid time to show and down here also in the console we have all of these errors so we need a way to combat this and to do this we can use in doubt what's known as a try and catch block where we try to do something but if that fails and there's an error we can then catch that error in a catch block and execute some different code so it looks something like this let me just do a try and then we do curly braces and we try something inside these curly braces ie this code down here now if that fails underneath we have a catch block which catches an error in parentheses and then we can execute some different code instead if we get an error okay so let's try this I'm going to grab all of this code right here and this is what we're going to try to do so I'm going to paste it in here and then we're going to try all of this now obviously this is gonna fail because this endpoint is incorrect we don't have an S in the endpoint and down here what we're going to do is catch the error and print it so let me print now this error object that dart has created for us based on this error so I'm gonna print a string which says caught error and then the actual error variable so let's see what happens now I'm going to start and by the way we will get an error over here now because now no longer are we setting the time variable now as soon as there's an error we're going on to this catch block and now we're not setting the time variable over here so therefore when we update the state over here we're setting this to be null and that is going to error out over here so let me now just come down here and go to hot restart and notice you'll get a massive error over here and also the same errors over here but if we scroll up now we should see at some point our little message there we go caught error right here that's what we print out and also the error message which is type list dynamic is not a sub type map dynamic so basically that's saying somewhere up here probably where we're trying to create this map this right here is not returning us a map so we can't do this so it's forcing an error because this is not returning the data that we expect so that's fine we've printed out that error message now in the console for us to see but also what we should do is something so this doesn't happen so what I'm going to do is update the time variable inside here and I'll set it to something like could not get time data okay so at least the user knows what's going on and they don't see this red screen of death or whatever so now if we were to save this what we're doing if this an error is coming to this catch block printing this message then updating the time variable to this message over here so we get that back over here and when we set the state we update it to be that message instead of loading and therefore that message is going to be output to the user so let me save all this go to both files and save go to run and then go to hot restarts cross your fingers and hope this works okay cool so now we can see this thing could not get time data now we don't get all of those other errors anymore because we're not trying to do something with that Nova you have time anymore that would cause those errors this time we're simply out point this could not get time data and we also have an error force in the console so we know what is going on and that is a better way to handle this error I think so now we've seen that let's just go back to here and take off the s and make sure this still works I'm gonna save it and and then going to run it and hopefully we should see the actual time over here now cool okay so that's error handling sorted for now and now we have this time data the loading screen is kind of almost serving its purpose it's loading up the data it says loading we're going to replace that with some kind of spinner later but it says loading the next thing to do is to redirect to the home page once we have this data so we can actually show this on the home page so we'll look at how we can pass this data from one widget to a different widget or rather from one routes or routes to a different route in the next video
Info
Channel: Net Ninja
Views: 136,702
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, flutter tutorial for beginners, flutter for beginners, tutorial, dart, dart tutorial, dart and flutter, dart flutter, dart tutorial for beginners, flutter vs react native, error handling, errors, api error, try catch, try, catch, dart errors, flutter error handling, dart error handling
Id: HemchBJQdgM
Channel Id: undefined
Length: 5min 29sec (329 seconds)
Published: Thu Sep 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.