'How to make final local variables non nullable?
The code below produces the error String? cannot be assigned to the parameter type 'String'
Some function() {
try{
...
final userData;
userData = json.encode({'token': _token, 'userId': user_Id, "expiryDate": _expiryToken });
prefs.setString("userData", userData);
} catch (error) {
throw error;
}
}
Future<bool>tryAutoLogin() async{
final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('userData')){
return false;
}
final extractedUserData;
if (prefs != null){
extractedUserData = json.decode(prefs.getString("userData")) as Map<String, Object>;
final expiryDate = DateTime.parse(extractedUserData['expiryDate']);
if(expiryDate.isAfter(DateTime.now())){
return false;
}
_token = extractedUserData['token'] as String?;
user_Id = extractedUserData['userId'] as String?;
_expiryToken = extractedUserData['expiryDate'] as DateTime?;
return true;
}
return false;
}
The line where there is decoding of json produces the error even after i added the line to make sure prefs is not null. What is the problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
