0

Fairly new to the world of Flutter and I've been searching for a built-in method that makes a partial match to one string value and an entire set of strings within a list. For example...

Let's say I am querying the string "Farmhouse Sides, Coleslaw".

determineFood(String category) {


List<String> categoryArray = <String>[];

var breakFastWords = ["waffle", "toast", "pancake", "omelette", "Omelet"];
var lunchWords = ["salad", "sandwich", "soup", "burger", "pasta", "gyro", "hoagie"];
var dinnerWords = ["burger", "steak", "pork", "ribs"];
var sideWords = ["dressing", "drink", "mustard", "jelly", "peanuts", "coleslaw"];

test(String value) => value.contains(category);


if (breakFastWords.any(test))
{
  categoryArray.add("Breakfast");
}

Even using the function with test(String value) as opposed to a straight comparison of the string with the list with "contains", I am unable to get a match unless the two strings match EXACTLY (i.e. just "coleslaw" and "coleslaw).

Would anyone be able to suggest a better way to go about this?

1
  • You can convert the two string to all lowerCase first then after match the two. Commented Mar 23, 2021 at 2:37

1 Answer 1

1

There are several ways to do this. One is very close to what you've done; however you need to call .toLowerCase() before matching the string. The other would be to use regex where you can tell it to ignore case.

Here's an example:

final breakfastWords = ["waffle", "toast", "pancake", "omelette", "Omelet"];
final lunchWords = ["salad", "sandwich", "soup", "burger", "pasta", "gyro", "hoagie"];
final dinnerWords = ["burger", "steak", "pork", "ribs"];
final sideWords = ["dressing", "drink", "mustard", "jelly", "peanuts", "coleslaw"];
final sideReg =  RegExp("(?:dressing|drink|mustard|jelly|peanuts|coleslaw)", caseSensitive: false);

bool isBreakfast(String string) {
  String toTest = string.toLowerCase();
  return breakfastWords.any((word) => toTest.contains(word));
}

bool isLunch(String string) {
  String toTest = string.toLowerCase();
  return lunchWords.any((word) => toTest.contains(word));
}

bool isDinner(String string) {
  String toTest = string.toLowerCase();
  return dinnerWords.any((word) => toTest.contains(word));
}

bool isSide(String string) {
  String toTest = string.toLowerCase();
  return sideWords.any((word) => toTest.contains(word));
}

bool isSideRegex(String string) {
  return sideReg.hasMatch(string);
}

void main() {
  String testString = "Farmhouse Sides, Coleslaw";
  
  if(isBreakfast(testString)) {
    print("It's breakfast");
  }
  
  if(isLunch(testString)) {
    print("It's lunch");
  }
  
  if(isDinner(testString)) {
    print("It's dinner");
  }
  
  if(isSide(testString)) {
    print("It's a side");
  }
  
  if(isSideRegex(testString)) {
    print("It's a side found with regex");
  }
}final breakfastWords = ["waffle", "toast", "pancake", "omelette", "Omelet"];
final lunchWords = ["salad", "sandwich", "soup", "burger", "pasta", "gyro", "hoagie"];
final dinnerWords = ["burger", "steak", "pork", "ribs"];
final sideWords = ["dressing", "drink", "mustard", "jelly", "peanuts", "coleslaw"];
final sideReg =  RegExp("(?:dressing|drink|mustard|jelly|peanuts|coleslaw)", caseSensitive: false);

bool isBreakfast(String string) {
  String toTest = string.toLowerCase();
  return breakfastWords.any((word) => toTest.contains(word));
}

bool isLunch(String string) {
  String toTest = string.toLowerCase();
  return lunchWords.any((word) => toTest.contains(word));
}

bool isDinner(String string) {
  String toTest = string.toLowerCase();
  return dinnerWords.any((word) => toTest.contains(word));
}

bool isSide(String string) {
  String toTest = string.toLowerCase();
  return sideWords.any((word) => toTest.contains(word));
}

bool isSideRegex(String string) {
  return sideReg.hasMatch(string);
}

void main() {
  String testString = "Farmhouse Sides, Coleslaw";
  
  if(isBreakfast(testString)) {
    print("It's breakfast");
  }
  
  if(isLunch(testString)) {
    print("It's lunch");
  }
  
  if(isDinner(testString)) {
    print("It's dinner");
  }
  
  if(isSide(testString)) {
    print("It's a side");
  }
  
  if(isSideRegex(testString)) {
    print("It's a side found with regex");
  }
}

Also note that you could form your regex directly from the strings:

final sideReg = RegExp("(?:${sideWords.join("|")})", caseSensitive: false);

Sign up to request clarification or add additional context in comments.

Comments

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.