i get int? variable from SharedPreferences class with getInt() method. I have to put this int? as a parameter for Duration Object but i need to have an int. What can I do? This is the code:
int? duration = ParametersHelper.getDuration();
Future.delayed(const Duration(milliseconds: duration));
This is getDuration() method in ParametersHelper:
static int? getDuration() {
return _pref.getInt("Duration");
}
Thank you!
You can either override this behavior by explicitly telling the dart compiler that although the variable can be nullable I guarantee that it's not null, be careful when using it though.
int duration = ParametersHelper.getDuration()!; // using the ! operator
Future.delayed(const Duration(milliseconds: duration));
The second option is to use the ?? operator which means if the value is null assign another value
int duration = ParametersHelper.getDuration() ?? 2; // if null assign 2
Future.delayed(const Duration(milliseconds: duration));