New flutter rules caused this ERROR: The argument type 'String?' can't be assigned to the parameter type 'String'
I have a code like this:
String? _token;
DateTime? _expiryDate;
String? _userId;
Timer? _authTimer;
Future<bool> tryAutoLogin() async {
final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('userData')) {
return false;
}
final extractedUserData =
json.decode(prefs.getString('userData')) as Map<String, Object>;// FIRST ERROR
final expiryDate = DateTime.parse(extractedUserData['expiryDate']);// SECOND ERROR
if (expiryDate.isBefore(DateTime.now())) {
return false;
}
_token = extractedUserData['token']; //THIRD ERROR
_userId = extractedUserData['userId']; // THIRD ERROR
_expiryDate = expiryDate;
notifyListeners();
_autoLogout();
return true;
}
But it gives me these errors:
The argument type 'String?' can't be assigned to the parameter type 'String'.
The argument type 'Object?' can't be assigned to the parameter type 'String'.
A value of type 'Object?' can't be assigned to a variable of type 'String?'. Try changing the type of the variable, or casting the right-hand type to 'String?'.
I found this code from a Flutter tutorial and tried to fix the errors by adding ?
or !
marks after some variable types but it seems I couldn't do that well.
EDIT: By updating this line of the code
final extractedUserData = json.decode(prefs.getString('userData')) as Map<String, Object>;
to
final extractedUserData = json.decode(prefs.getString('userData')) as Map<String, dynamic>;
The first error still exists but the other errors gone. I also tried to update the line like
final extractedUserData = json.decode(prefs!.getString('userData')) as Map<String, dynamic>;
(changed prefs
to prefs!
) but couldn't help, and I don't know how to fix it?
from Recent Questions - Stack Overflow https://ift.tt/3umKv9j
https://ift.tt/eA8V8J
Comments
Post a Comment