compareOfVariable method Null safety

List compareOfVariable(
  1. List list,
  2. String operator,
  3. dynamic element
)

Implementation

List compareOfVariable(List list, String operator, var element) {
  if (element == null) {
    throw new Exception(
        "DartTensorException : element not found for comparision.");
  } else if (element is List) {
    throw new Exception("DartTensorException : List is not allowed.");
  } else {
    List opratorList = ['<', '>', '>=', '<=', '==', '!='];
    List shape = getDim(list);
    List temp = [];
    if (!opratorList.contains(operator)) {
      throw new Exception(
          "DartTensorException : Unrecognised comparision operator.");
    } else {
      if (operator == '<') {
        temp = flatten(list).map((e) => e < element ? true : false).toList();
      } else if (operator == '>') {
        temp = flatten(list).map((e) => e > element ? true : false).toList();
      } else if (operator == '>=') {
        temp = flatten(list).map((e) => e >= element ? true : false).toList();
      } else if (operator == '<=') {
        temp = flatten(list).map((e) => e <= element ? true : false).toList();
      } else if (operator == '==') {
        temp = flatten(list).map((e) => e == element ? true : false).toList();
      } else if (operator == '!=') {
        temp = flatten(list).map((e) => e != element ? true : false).toList();
      }
    }
    temp = generate(temp, shape);
    return temp;
  }
}