I saw this operator in Example of Shared Preferences for Flutter plugin. And I didn't get it.
int counter = (prefs.getInt('counter') ?? 0) + 1;
It is called null-aware operator
Meaning, if and only if prefs.getInt('counter') returns null assign 0 to it and then increment by one.
Here, you can find a great blog post about null-aware operator: http://blog.sethladd.com/2015/07/null-aware-operators-in-dart.html
Related
I have two int values in Dart, test them equals or not with == previously, it was fine. Now I have to test if first one is greater or equals to the second one, so I changed == to >= instinctively, but it seems Dart was not happy with me doing that
I double checked documentation of Dart, >= should work to test greater or equals operation
IMO, in the end, if >= has something wrong, then the same should come to ==
You are comparing two different operators which comes from different part of the Dart language. The == operator is defined on Object:
https://api.dart.dev/stable/2.15.1/dart-core/Object/operator_equals.html
Which needs to be able to compare every type of object including null. The reason why the == operator does not have Object? as input parameter is because of the following detail in the Dart Language Specification (page 167) under the section "Equality":
Evaluation of an equality expression ee of the form e1 == e2 proceeds as follows:
The expression e1 is evaluated to an object o1.
The expression e2 is evaluated to an object o2.
If either o1 or o2 is the null object (17.4), then ee evaluates to true if both o1 and o2 are the null object and to false otherwise. Otherwise,
evaluation of ee is equivalent to the method invocation o1.==(o2)
...
https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf
So based on third rule, the language itself will make sure that null values are compared correctly and therefore the Object.== method will never see a null object as its input.
The other operator is >= and comes (in your case) from num which int extends from:
https://api.dart.dev/stable/2.15.1/dart-core/num/operator_greater_equal.html
And is specified to accept only num (so int or double) which are a non-nullable type (null is therefore not allowed).
You are then trying to call the num.>= operator with the result from values?.length. This is a problem since values is nullable and can therefore potentially be null. And when you use the ?. operator, you are saying that if values are null, don't care calling .lenght but instead just return null.
So the type of values?.length ends up being int? which is not compatible with num.
This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
I actually don't know how to take a integer input from the input console. Then I tried this after a little research.
My Code:
import 'dart:io';
void main() {
stdout.write("Enter a number: ");
int num1 = int.parse(stdin.readLineSync());
print(num1);
}
But it doesn't work, showing an error message,
ERROR: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
Then finally I came to know that in dart2.12+ versions dart introduced null safety. Any suggestion to do it properly in the null safety environment.
The readLineSync() method returns a String?. The ? symbol indicates this variable may be null.
On the other hand, the int.parse() method expects a String, without the ? symbol. This means it doesn't know how to handle if the result from the readLine method comes null.
The easiest way to solve this is to give a default value in case the result comes null:
int num1 = int.parse(stdin.readLineSync() ?? '0');
The ?? operator makes the expression evaluates to the right side, if the left side is null. So giving it a default value it won't have to bother with a nullable type.
There are other operators you can use. You can read more about it in the Dart documentation about it.
Try this
import 'dart:io';
void main()
{
// Asking for favourite number
print("Enter your favourite number:");
// Scanning number
int n = int.parse(stdin.readLineSync());
// Printing that number
print("Your favourite number is $n");
}
If this not work I think you should try this answer link here
In Dart the null-aware operator for methods in combination with the ?? operator does not work well.
Image having a call chain like this:
object.getter.getter.getter.getter
Now when object could be null and I do not want an exception I have to write this:
object?.getter?.getter?.getter?.getter
That is a null check for every getter, even though only object can be null, but then the getter's obviously do not work.
I am okay with this. But now I have a scenario, where I want to return another value if null, but I am working with a list:
list[index][index][index]
How do I use the null-aware operator on the List getter?
This does not work:
list?[index]
and this does not exist for List's in Dart.
list?.get(index)
I want to achieve something like this:
list?[index]?[index]?[index] ?? 0
Where I know that only list will ever be null.
This long code would work:
list == null ? 0 : list[index][index][index]
There is no null-aware operator for the indexing operator ([])
You can use elementAt() instead:
list?.elementAt(index)?.elementAt(index)?.elementAt(index) ?? 0
UPDATE - 2021/05/24
If you are using null-safety, you can now use this:
list?[index]?[index]?[index] ?? 0
Anyone came across this?
I ve got a nice little loop but it seems to get a warning.
for(;;nimages++)
It was proposed and accepted to remove the ++ and -- operators from the Swift language in an upcoming release, therefore the warning you're seeing is to help you avoid these types of constructs in your code before it is removed. (Please reference the link for a full explanation as well as the advantages and disadvantages that they provide.)
Please note that C-Style loops will also be deprecated in the near future according to the following proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md
Not knowing exactly what kind of logic you need to implement I don't feel confident recommending a solution, however in accordance with the above proposals I would recommend that you may want to become familiar with the Swift for-in and stride statements. Or, as another person recommended, using a while loop may also be appropriate.
What's the question? The entire error message is
C-style for statement is deprecated and will be removed in a future version of
Swift
You could replace this with something like
while true {
// loop body
nimages += 1
}
Finally, if you know the number of iterations you want, you can use a for-in loop:
for nimages in 0..<maxImages { /* loop body */ }
C Style For loops are deprecated and incrementing decrementing like these i++ , i-- also deprecated
Therefore couldn't be used this type of loops anymore
let myArray = ["one","two","three","four","five","six"]
for var i = 0; i < myArray.count; i++ {
print(myArray[i])
}
Instead of use above syntax, we can use this
let myArray = ["one","two","three","four","five","six"]
for i in 0..<myArray.count {
print(myArray[i])
}
And also this
for i in 0...myArray.count-1 {
print(myArray[i])
}
If you are not familiar with Range Operators and Half-Open Range Operators this is the time
(Document Link)
Range Operators
The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.
Half-Open Range Operators
The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b.
For index-- style
for index in 10.stride(to: 0, by: -1) {
print(index)//This is 10, 9, 8, ... 1 NOT 0
}
For index++ style
for index in 0..<10 {
}
In my dissector I have this code
local defaultdata = data_tvb():bytes()
local newdata = ByteArray.new()
newdata:set_size(defaultdata:len())
for i=0,defaultdata:len()-2 do
local var = bit.band((bit.lshift(defaultdata:get_index(i), 1) + bit.rshift(defaultdata:get_index(i+1), 7)), 0xff)
newdata:set_index(i, var) end
local var = bit.band((bit.lshift(defaultdata:get_index(defaultdata:len()-1), 1) + bit.rshift(defaultdata:get_index(0), 7)), 0xff)
newdata:set_index(defaultdata:len()-1,var)
data_tvb = ByteArray.tvb(newdata, "Decoded") end
My problem is in second bitwise operation in get_index function.
I know, that problem might be in get_index(0) or get_index(defaultdata:len()-1) because in Lua there is no element of the zero index(not that of C) but nothing actually works with another values.
With any values I got this message: bad argument #1 to 'get_index' (index out of range)
So, as I mentioned above, part, that not depend on this code work correctly.
Almost forgot to post solution.
The matter is that my dissector don't cover cases of null application protocol payload, and thats why i got these errors. The solution is to add one "if" statement, that checks out length of the payload.