如何在flutter中编码int列表



如何在flutter中编码int列表?

static Future<void> setIdList(List<int> idList) async {
var box = Hive.box(HIVE_DB_NAME);
String jsonData = json.encode(
List<int>.from(
idList.map(
(e) => e.toString(),
),
),
);
await box.put(ID_kEY, jsonData);
}

正如你所看到的,我已经尝试了toString()方法,但它给了我一个错误:

Unhandled Exception: type 'String' is not a subtype of type 'int'

发生此错误是因为您设置了类型为int的列表,但实际上您通过调用toString方法创建了类型为type的列表。要解决这个问题,请将列表的类型更改为String

static Future<void> setIdList(List<int> idList) async {
var box = Hive.box(HIVE_DB_NAME);
String jsonData = json.encode(
List<String>.from(
idList.map(
(e) => e.toString(),
),
),
);
await box.put(ID_kEY, jsonData);
}

最新更新