简体   繁体   English

将列表与Dart / Flutter中的值进行比较

[英]Compare List with Value in Dart / Flutter

I am trying to compare an input value to a list of items in Dart language, however, it does not seem to work. 我正在尝试将输入值与Dart语言中的项目列表进行比较,但是,它似乎不起作用。

Method: 方法:

  String validateStoreNumber(String value) {

    List<String> storeList = ['55', '56', '88'];

    // String patttern = r'(^[a-zA-Z ]*$)';
    RegExp regExp = new RegExp(r'^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$');
    if (value.length == 0) {
      return "car number is required";
    } else if (!regExp.hasMatch(value)) {
      return "Accepts only numbers";
    } else if (value.length != 2){

      return "car number must have 2 digits";
    } else if (value != storeList.contains(value)){

      return "Not our model";
    }
    return null;
  }

This line... 这条线...

else if (value != storeList.contains(value))

Is the problem. 是问题。 The left side "value" is a string, and the right side "contains" is a boolean. 左侧的“值”是一个字符串,而右侧的“包含”是一个布尔值。 You are comparing a string to a boolean. 您正在将字符串与布尔值进行比较。

I think you want this... 我想你要这个...

else if (!storeList.contains(value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM