Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isDaytime cannot be null - uncaught exception is thrown when user has no internet connection #27

Open
matinayo opened this issue Sep 22, 2020 · 2 comments

Comments

@matinayo
Copy link

from lesson 33

when setting up the background image using the ternary operator, if a user does not have an internet connection it causes the app to crash instead of displaying the info 'Could not get time data'. Although a try and catch block was added to the world_time.dart class to prevent the app from crashing when it fails to fetch json data, the boolean variable which is 'isDaytime' would be null because it would skip the try block and when it is being called in the loading.dart file it would have a value of null which would cause another exception in the home.dart file when setting the bgImage variable which depends on the isDaytime variable.


The code below should be added to home.dart file just above the scaffold widget
String bgImage;
//isDayTime can be null if time data is not found (no internet connection)
if(data['isDayTime'] != null){
//set background image
bgImage = data['isDayTime'] ? 'day.png' : 'night.png';
}else{
//set the bgImage to 'day.png'
bgImage = 'day.png';
}

@matinayo
Copy link
Author

This is just an uncaught exception I noticed in the world time app

@tahir502
Copy link

Nice catch. Wouldn't it be better to just set isDayTime to false by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants