Flutter将ListMap< int>到List< String>抛出错误“String”不是类型“int”的子类型

6l7fqoea  于 2022-11-25  发布在  Flutter
关注(0)|答案(2)|浏览(180)

未行程的例外状况:型别'String'不是型别转换中型别'int'的子型别

else if (value is List<int>) {
          prefs.setStringList(
              "itemsToLoanCats", [...value.map((e) => e.toString())]);
        }

“值”的类型= List<int>
我不明白为什么这不起作用,我使用.toString()
堆栈跟踪

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)

增加:

List<String> encodedList = [];
  for(int intValue in (value as List<int>)) {
    print(LOG + "intValue: $intValue");
    encodedList.add(int.parse(intValue).toString());
  }

也不起作用,引发相同的异常。
按要求提供更长的代码片段:

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())]);
        }
vc9ivgsu

vc9ivgsu1#

首先尝试将列表编码为json。

encodedList = json.encode(value);
debugPrint(encodedList.toString());

prefs.setString('itemsToLoanCats', encodedList);
w7t8yxp5

w7t8yxp52#

我发现value的类型与我的字符串Map代码不兼容。
原来value是用.cast<int>()赋值的,这不会产生一个合适的List,而是一个Map?CastList<String, int>
无论如何,我不得不在代码的前面进行深入研究,并重新配置从prefs中提取变量时如何赋值。
(prefs.getStringList('itemsToLoanCats') ?? []).map(int.parse).toList();
解决了。
感谢大家的关注和鼓励。如果你想发表一个答案,解释为什么.cast<int>生成一个Map,而不能像List<int>一样处理,那么我会给答案的信用。
非常奇怪,它报告为List<int>,但拒绝这样做🤔

相关问题