gustave
gustave

Reputation: 1

_CastError (type 'String' is not a subtype of type 'int?' in type cast) Flutter

My app Can't start due to this error in Flutter:

Error

final Map<String, Object> _preferenceCache;

  /// Returns all keys in the persistent storage.
  Set<String> getKeys() => Set<String>.from(_preferenceCache.keys);

  /// Reads a value of any type from persistent storage.
  Object? get(String key) => _preferenceCache[key];

  /// Reads a value from persistent storage, throwing an exception if it's not a
  /// bool.
  bool? getBool(String key) => _preferenceCache[key] as bool?;

  /// Reads a value from persistent storage, throwing an exception if it's not
  /// an int.
  int? getInt(String key) => _preferenceCache[key] as int?;

  /// Reads a value from persistent storage, throwing an exception if it's not a
  /// double.
  double? getDouble(String key) => _preferenceCache[key] as double?;

  /// Reads a value from persistent storage, throwing an exception if it's not a
  /// String.
  String? getString(String key) => _preferenceCache[key] as String?;

I tried to add .toString() but still don't work.

Upvotes: 0

Views: 394

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63594

_preferenceCache[key] giving you String. Instead of using as it would be better to use tryParse, you can convert to int like

 int? getInt(String key) => int.tryParse("${_preferenceCache[key]}");

More about tryParse

Upvotes: 2

Related Questions