In Flutter/Dart, what is the difference between using "==" vs "is" in a conditional if statement [duplicate] - flutter

This question already has answers here:
What is the difference between 'is' and '==' in Dart?
(2 answers)
Closed 2 years ago.
I am doing a tutorial on BLoC in Flutter and the tutor uses the keyword "is" in his conditional if statement, which he said "smartcasts" the state. Can anyone help me understand how the "is" operator gives me access to the bloc's state in the code below?
BlocBuilder<WeatherBloc, WeatherState>(
builder: (context, state) {
if (state is WeatherLoaded) {
return buildColumnWithData(context,state.weather);
}
When I tried the same code with if (state == WeatherLoaded), I'm not able to pass the state.weather into the buildColumnWithData function. Why is this?

'==' is an equality operator.
To test whether two objects x and y represent the same thing, use the == operator. (In the rare case where you need to know whether two objects are the exact same object, use the identical() function instead.) Here’s how the == operator works:
If x or y is null, return true if both are null, and false if only one is null.
Return the result of the method invocation x.==(y). (That’s right, operators such as == are methods that are invoked on their first operand. For details, see Operators.)
'is' is a type test operator
The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.
In your code:
is checks if state is instance of WeatherLoaded class.
On top of that, you don't need to make a cast to WeatherLoaded if the check was successful - in the scope of the if statement state variable is downcast to WeatherLoaded ("is smartcasts the state").
With == operator, you can compare 2 instances of a class.

Related

Conditionally execute a funtion, throws error `Conditions must have a static type of 'bool'. Try changing the condition`

I want to feed Two to the function. When function receives Two as the data, it should print success to the console. But seems like it's not the correct way.
Error: Conditions must have a static type of 'bool'. Try changing the condition.
List
enum ButtonList {One, Two, Three}
Calling function
testFunc(ButtonList.Two)),
Function
testFunc( ButtonList type) {
if (type = ButtonList.Two ){print('sucess ')};
}
It should be:
testFunc(ButtonList type) {
if (type == ButtonList.Two) {
print('sucess ')
};
}
There's a big difference between = (assigning a value to a variable) and == (equality comparison). if expects a condition (==), not the assigning operation (=).
Formatting is important to read and understand the code. Please read Dart best code style practices: https://dart.dev/guides/language/effective-dart/style
You are trying to assign with =, use == instead

Dart function's return Objects not Equal [duplicate]

This question already has answers here:
How can I compare Lists for equality in Dart?
(14 answers)
Closed 1 year ago.
I am new to flutter and Dart language. Can someone please explain to my why geek1==geek2 is false when it should be true by principle. function gfg() returns same value everytime then why geek1 and geek2 are not equal?
gfg() => [1, 2];
// Main function
void main() {
// Assiging value
// through function
var geek1 = gfg();
var geek2 = gfg();
// Printing result
// false
print(geek1 == geek2);
print(geek1);
print(geek2);
}
Object equality is evaluated by reference in Dart - Basically, you are creating two different objects, which are stored at two different memory addresses. When the Dart runtime compares the objects, it compares their memory addresses, rather than their content by default.
To compare two List objects in terms of their contents, you can use the listEquals function.
Some extra info:
To compare classes for equality, you can override the equality operator, or you can use the Equatable package.
Because gfg is a function which returns a new array on every call.
You're comparing them with ==, which is the operator for doing identity comparison, not value comparison (a.k.a. equality). Since the instances are always distinct, they're never identical.

dart gRPC: what the meaning of the function?

I'm new in flutter(dart) gRPC. I'm learing the tutorial given by https://grpc.io/docs/languages/dart/basics/. But I got confused about the dart syntax in this function.
Future<Feature> getFeature(grpc.ServiceCall call, Point request) async {
return featuresDb.firstWhere((f) => f.location == request,
orElse: () => Feature()..location = request);
}
Actually, I don't understand what argument f means and why there is an orElse. I have found => means arrow function and it can be simply understood as return sentence, but I can't say I figure it out toally. Any explanation would be appreciated.
firstWhere method takes a Predicate. A Predicate is just a function that takes in an object, and returns true or false. So basically it's saying "give me the first object from this list where the function I'm giving you returns true. The orElse is an optional, named parameter that says, if you've gotten to the end of the list and not a single object returned true when passed through the function I just supplied, then execute this function as a last resort and return whatever value it produces. You can think of a Predicate like a filter. It takes an object and returns true if it should pass through the filter, or false if it should not pass through the filter. firstWhere basically goes through each element checking to see if it passes through the filter, and the first time something does, it returns that element. If nothing makes it through the filter, it uses the orElse producer function to generate some value to return, since nothing made it through on it's own.
(f) => f.location == request is a function that returns true or false based on it's argument - it's a Predicate
() => Feature()..location = request is a Producer. A function that has no argument, but produces a value. In this case, a value that is equal to a new Feature with a location value equal to request. An assignment evaluates to the value that was assigned. The cascade .. ensures that the Feature will be returned, instead of the Point object, request.
So basically you can think of it like this:
list.giveMeTheFirstObjectWhere(thisFunctionReturnsTrue, orElse: giveMeTheValueThisFunctionProvidesIfNoneOfTheElementsReturnedTrueUsingTheOtherFunction)
So the purpose of this code seems to be, checking if a Feature already exists, and if it does, it returns the first such Feature. If it doesn't exist, it creates a new Feature and returns it (however, this newly created one isn't automatically added to the list/db)

how to handle empty array for text widget in build method?

How would I handle the objectA[0].name (a string) in the build method if the array is empty?
Text(objectB.objectC.objectA[0].name),
Assuming the array is objectC you can do something like:
Text(objectC.isEmpty? "" :objectC[0].name)
You can read more about ternary operators in dart here
There isn't a great way to do this inline but with a simple extension method to return the original null or the iterable depending on whether the item is null or empty you can make it work.
First
Extension Method
(requires dart v2.7 - update in your pubspec.yaml file)
extension IterableExtension<T> on Iterable<T> {
Iterable<T> get nullWhenEmpty =>
this == null || this.isEmpty ? null : this;
}
Second
To handle null values while you're traversing an object you can use the Dart's conditional member access operator (?.). This operator will only continue with the right-hand side if the left-hand side of the operator is not null. Use the elementAt method on a an iterable to be able to use the ?. operator in the chain. Then, use the ?. operations with the if null operator (??) to get your default value.
Solution
final String value = objectB?.objectC?.objectA?.nullWhenEmpty?.elementAt(0)?.name;
Text(value ?? 'Default Text');
You can, of course, inline the above code instead of using an additional variable.
Resources
Dart Language Tour: Other Operators
Dart Language Tour: Classes
Maybe checking if the list has an element, using isNotEmpty
child: (objectB.objectC.objectA.isNotEmpty)
? Text(objectB.objectC.objectA[0].name)
: Container(),

ternary in postlude?

Can I do ternary in the postlude(s) the same way that I do them int he prelude? In other words have it fire off different calls depending on the say something was evaluated in the prelude?
Yes. You can include a guard condition on a postlude expression. This works for both explicit event raising and persistant variable modification. The expression following if can be any valid expression, including functions.
Example:
fired {
raise explicit event "foo" if cheese == 5;
}