iamshaunjp / iamshaunjp/flutter-beginners-tutorial

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

Open
#27 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

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';
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.