this is my code and getting these error:
- The operator '<' can't be unconditionally invoked because the receiver can be 'null'.
- The argument type 'int?' can't be assigned to the parameter type 'num'.
code
void main()
{
int romanToInt(String s) {
s=s;
Map<String,int> roman = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
};
int result=0;
for(int i=0;i<s.length;i ){
if(i 1<s.length && roman[s[i]]<roman[s[i 1]])
{
result-= roman[s[i]];
}else{
result = roman[s[i]];
}
}
print(result);
return result;
}
}
CodePudding user response:
your code has 2 problems, 1st null safety and 2nd The argument type 'int?' can't be assigned to the parameter type 'num'.
your code can change like this:
int romanToInt(String s) {
s=s;
Map<String,int> roman = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
};
int result=0;
for(int i=0;i<s.length;i ){
if(i 1<s.length && roman[s[i]]!<roman[s[i 1]]!.toInt())
{
result-= roman[s[i]]!.toInt();
}else{
result = roman[s[i]]!.toInt();
}
}
print(result);
return result;
}
CodePudding user response:
Try this:
void main()
{
int romanToInt(String s) {
s=s;
Map<String,int> roman = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
};
int result=0;
for(int i=0;i<s.length;i ){
if(i 1<s.length && roman[s[i]]!<roman[s[i 1]]!)//<- add null check
{
result-= roman[s[i]]!; //<- add null check
}else{
result = roman[s[i]]!;// <- add null check
}
}
print(result);
return result;
}
}