Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
else if (value is List<int>) {
prefs.setStringList(
"itemsToLoanCats", [...value.map((e) => e.toString())]);
}
type of 'value' = List<int>
I don't see why this doesn't work, I'm using .toString()
STACK TRACE
I/flutter (16996): Person This is claiming to be itemsToLoanCats: [0]
I/flutter (16996): saveToPrefs converting GeoPoint...
E/flutter (16996): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
E/flutter (16996): #0 _CastListBase.[] (dart:_internal/cast.dart:99:46)
E/flutter (16996): #1 ListMixin.elementAt (dart:collection/list.dart:78:33)
E/flutter (16996): #2 ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
E/flutter (16996): #3 StringBuffer.writeAll (dart:core-patch/string_buffer_patch.dart:96:19)
E/flutter (16996): #4 IterableBase.iterableToFullString (dart:collection/iterable.dart:268:14)
E/flutter (16996): #5 ListMixin.toString (dart:collection/list.dart:588:37)
E/flutter (16996): #6 Person.saveToPrefs.<anonymous closure> (package:meloan/model/person.dart:635:72)
E/flutter (16996): #7 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
E/flutter (16996): #8 Person.saveToPrefs (package:meloan/model/person.dart:593:20)
E/flutter (16996): #9 _PersonalDetailsScreenState._buildBody.<anonymous closure> (package:meloan/personal/edit_personal_details.dart:1231:30)
E/flutter (16996): #10 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1072:21)
E/flutter (16996): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
E/flutter (16996): #12 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
E/flutter (16996): #13 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
E/flutter (16996): #14 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:276:7)
E/flutter (16996): #15 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:163:27)
E/flutter (16996): #16 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:464:20)
E/flutter (16996): #17 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:440:22)
E/flutter (16996): #18 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:337:11)
E/flutter (16996): #19 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:395:7)
E/flutter (16996): #20 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:357:5)
E/flutter (16996): #21 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:314:7)
E/flutter (16996): #22 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:295:7)
E/flutter (16996): #23 _invoke1 (dart:ui/hooks.dart:167:13)
E/flutter (16996): #24 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:341:7)
E/flutter (16996): #25 _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
ADDITION:
List<String> encodedList = [];
for(int intValue in (value as List<int>)) {
print(LOG "intValue: $intValue");
encodedList.add(int.parse(intValue).toString());
}
Doesn't work either, same Exception thrown.
Longer code snippet as requested:
saveToPrefs(SharedPreferences prefs) {
(this.toMap()).forEach((key, value) {
//print("saveToPrefs entered with key: $key\nvalue: $value");
if (value != null) {
if (key == "dbProfilePicPath") {
print("PERSON " "should be saving dbProfilePicPath");
}
if (value is String) {
prefs.setString(key, value);
} else if (key == "position") {
print("saveToPrefs converting GeoPoint...");
double latitude = value['geopoint'].latitude;
double longitude = value['geopoint'].longitude;
prefs.setDouble("latitude", latitude);
prefs.setDouble("longitude", longitude);
} else if (value is List<String>)
prefs.setStringList(key, value);
else if (value is double)
prefs.setDouble(key, value);
else if (value is int)
prefs.setInt(key, value);
else if (value is bool)
prefs.setBool(key, value);
else if (value is Map<String, String>) {
// needs to be stored as a list
List<String>? list = [];
value.forEach((key, value) {
list.add("$key:$value");
});
prefs.setStringList(key, list);
} else if (value is Map<String, bool>) {
// e.g. .Charateristics needs to be stored as a list
List<String>? list = [];
value.forEach((key, value) {
list.add("$key:${value.toString()}");
});
prefs.setStringList(key, list);
} else if (value is Map<String, dynamic>) {
// loanItemFinancials / loanItemDetails?
//prefs.setString('loanItemDetails', json.encode(value));
} else if (value is List<int>) {
// itemsToLoanCats, each loaning category encoded to int
value = value.cast<int>();
print(LOG 'This is claiming to be itemsToLoanCats: ${value.toString()}');
prefs.setStringList("itemsToLoanCats", value.map((el) => el.toString()).toList());
//prefs.setStringList("itemsToLoanCats", [...value.map((e) => e.toString())]);
}
CodePudding user response:
Try encoding the list into json first.
encodedList = json.encode(value);
debugPrint(encodedList.toString());
prefs.setString('itemsToLoanCats', encodedList);
CodePudding user response:
OK solved. So I discovered the type of value
wasn't compatible with my map to String code.
Turns out value
was assigned using .cast<int>()
, this doesn't produce a proper List, it's a Map? CastList<String, int>
.
Anyway, I had to delve earlier in my code and reconfigure how this variable is assigned when extracted from prefs
.
(prefs.getStringList('itemsToLoanCats') ?? []).map(int.parse).toList();
Solved it.
Crikey! Thanks for the attention and encouragement all. If you want to post an answer explaining why .cast<int>
produces a map and can't be treated like a List<int>
then I'll give the answer credit.
Extremely strange it reports as List<int>
but refuses to behave as such