'How do i define < operator on dart?

when i run the program,it shows "Failed to build basics1_dart:basics1_dart: bin/basics1_dart.dart:38:12: Error: The operator '<' isn't defined for the class 'String?'.
Try correcting the operator to an existing operator, or defining a '<' operator. if(num1<0)" what should i do



Solution 1:[1]

You are asking about String? type, which is a String with added possibility to contain null value.

Here under the question is a comment from @jamesdlin, it possible could be an answer, but operator < does not exist also for a String type. There is a compareTo function, which can be used instead to compare it with other String: if (num1 != null && num1.compareTo("0") < 0)

You can easily compare Your value in case of it does not actually contain the null. As have said @jamesdlin, You can compare you value with null first, but if You are sure it is not null, also You can use ! operator to write compact cast of Your value to not nullable: if (num1!.compareTo("0") < 0). This cast will throw an exception when it is null, but if You sure - why not?

Also I wonder why Your num1 variable is a nullable string while You compare it with a number? May be You need to cast to a numeric type first? You can use int.parse(String) to do this, and store parsed integer value in another variable, or use it once for comparing if (int.parse(num1!) < 0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nashev