0

I am trying to compare an input value to a list of items in Dart language, however, it does not seem to work.

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;
  }
6
  • What is the input and what is the expected output? What does "does not seem to work" mean? Commented Jul 24, 2018 at 16:31
  • 1
    This line } else if (value != storeList.contains(value)){ looks odd. I'd expect it to be } else if (!storeList.contains(value)){ or similar. What is this code supposed to do? Commented Jul 24, 2018 at 16:32
  • That is a validation method. I have a form that takes an input (value in this case), validates it and then takes the user to another screen. I am trying to get this input and compare it against a hard coded list of numbers as in storeList ? Commented Jul 24, 2018 at 16:33
  • That worked, actually ! Thanks. Commented Jul 24, 2018 at 16:35
  • 1
    That was the whole problem. But now it works, Dart syntax is a little bit different that what I am used to. Thanks a lot ! Commented Jul 24, 2018 at 17:17

1 Answer 1

4

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, correct answer has been provided by Günter Zöchbauer, but you added explanation as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.