首页 > 解决方案 > 将列表布尔值转换为列表字符串颤动

问题描述

如何将列表字符串转换为列表布尔值?

List<String> listString = ["true", "false, "true"]

List<bool> listBool = []

我试过这个

listBool = listString.map((f)=>(f.contains("true")? listBool.add(true): listBool.add(false)));

我的预期结果是

listBool = [true, false, true]

标签: flutterbooleanconverters

解决方案


您可以尝试使用 forEach 方法,如下所示:

    List<String> listString = ["true", "false", "true"];
    List<bool> listBool = <bool>[];
    listString.forEach((item) => item == "true" ? listBool.add(true) : listBool.add(false));

推荐阅读