【问题标题】:Convert list boolean to list string flutter将列表布尔值转换为列表字符串颤动
【发布时间】:2021-09-07 13:10:31
【问题描述】:

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

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]

【问题讨论】:

    标签: flutter boolean converters


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以使用map

      List<String> listString = ["true", "false", "true"];
      List<bool> listBool = listString.map((e) => e == "true").toList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-12
        • 2011-12-22
        • 2020-03-22
        • 2018-05-24
        • 1970-01-01
        • 2018-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多