isHeaderRowWithVariables function

bool isHeaderRowWithVariables(
  1. String line,
  2. List<String> varNames
)

Checks if a line is a header row by matching its contents against known variable names.

This compares the words in the line with the list of variable names extracted from the dataset. The line is considered a header row only if ALL words match variable names.

Implementation

bool isHeaderRowWithVariables(String line, List<String> varNames) {
  // Trim the line and split into words.

  String trimmedLine = line.trim();
  List<String> words = trimmedLine.split(RegExp(r'\s+'));

  // Check that every word matches a variable name.

  for (String word in words) {
    // Clean up the word (remove any punctuation that might be present).

    String cleanWord = word.replaceAll(RegExp(r'[^\w\d_]'), '');

    if (cleanWord.isEmpty) continue;

    // If any word doesn't match a variable name, this isn't a header row

    if (!varNames.contains(cleanWord)) {
      return false;
    }
  }

  // All words matched variable names (and we had at least one word).

  return words.isNotEmpty;
}