skip to Main Content

I have a problem that the argument type string can’t be assigned to the parameter type pattern in this code I added the null check ! but the error didn’t disappear what shall I do? in this problem i coufused the error in this line
caculStr = caculStr.replaceFirst(inlayMethod.firstMatch(caculStr)?.group(0), ‘caculStrTemp$i’);

  Future<dynamic> handleCalcuStr(String caculStr) async {
var temp = new Map();
if (matrixs.containsKey(caculStr)) {
  return matrixs[caculStr];
}
if (dbs.containsKey(caculStr)) {
  return dbs[caculStr];
}
if (num.tryParse(caculStr) != null) {
  return num.tryParse(caculStr);
}
if (ufTemp.containsKey(caculStr)) {
  return ufTemp[caculStr];
}
caculStr = formatCmdStr(caculStr);
if (caculStr.contains('(')) {
  var caculStrs = [];
  int index = -1;
  List<String> stack = [];
  for (int i = 0; i < caculStr.length; i++) {
    if (caculStr[i] == '(') {
      if (stack.length == 0) {
        caculStrs.add(new StringBuffer());
        index++;
      }
      stack.add('(');
      if (stack.length == 1) {
        continue;
      }
    }
    if (caculStr[i] == ')') {
      stack.removeLast();
      if (stack.length == 0) continue;
    }
    if (stack.length != 0) {
      caculStrs[index].write(caculStr[i]);
    }
  }

  if (caculStrs.length != 0) {
    for (int i = 0; i < caculStrs.length; i++) {
      RegExp inlayMethod = new RegExp(r'[A-Za-z]+(' +
          caculStrs[i]
              .toString()
              .replaceAll('+', '\+')
              .replaceAll('*', '\*')
              .replaceAll('(', '\(')
              .replaceAll(')', '\)') +
          r')');
      if (inlayMethod.hasMatch(caculStr)) {
        temp['caculStrTemp$i'] = await _invocationMethod(
            inlayMethod.firstMatch(caculStr)!.group(0).toString());
        caculStr = caculStr.replaceFirst(inlayMethod.firstMatch(caculStr)?.group(0), 'caculStrTemp$i');
        continue;
      }
      caculStr = caculStr.replaceFirst(
          '(' + caculStrs[i].toString() + ')', 'caculStrTemp$i');
      temp['caculStrTemp$i'] =
          await handleCalcuStr(caculStrs[i].toString());
    }
  }
}
 

2

Answers


  1. This is because replaceFirst expects a Pattern instead of a String as its first argument. The Pattern class is thesuper class of RegExp class. So you can pass a RegExp obkect as the first argument to the replaceFirst method.
    Try changing that line to

    caculStr = caculStr.replaceFirst(RegExp(inlayMethod.firstMatch(caculStr)?.group(0)), 'caculStrTemp$i');
    

    Shoud fix it.

    Login or Signup to reply.
  2. The root issue seems to be that replaceFirst() expects a RegExp pattern as the first argument, but we are passing it a string by calling inlayMethod.firstMatch(caculStr).group(0).

    To fix this, we need to pass the actual RegExp pattern itself to replaceFirst(), not the matched string:

    if (inlayMethod.hasMatch(caculStr)) {
      RegExpPattern pattern = inlayMethod.pattern;
      
      temp['caculStrTemp$i'] = await _invocationMethod(
          inlayMethod.firstMatch(caculStr)!.group(0).toString());
          
      caculStr = caculStr.replaceFirst(pattern, 'caculStrTemp$i');
    
      continue;
    }
    

    Here we get the pattern directly from the RegExp itself and pass that to replaceFirst().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search