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
This is because
replaceFirst
expects a Pattern instead of a String as its first argument. ThePattern
class is thesuper class ofRegExp
class. So you can pass aRegExp
obkect as the first argument to thereplaceFirst
method.Try changing that line to
Shoud fix it.
The root issue seems to be that
replaceFirst()
expects a RegExp pattern as the first argument, but we are passing it a string by callinginlayMethod.firstMatch(caculStr).group(0)
.To fix this, we need to pass the actual RegExp pattern itself to
replaceFirst()
, not the matched string:Here we get the
pattern
directly from the RegExp itself and pass that toreplaceFirst()
.