Shorthand for calling a nullable callback in Dart? [duplicate] - dart

This question already has answers here:
Null aware function invocation operator
(2 answers)
Closed 1 year ago.
Say, I have this:
VoidCallback callback;
It can only be called if it isn't null, so for that I am doing:
if (callback != null) callback();
Is there any shorthand similar to this?
callback?.();

Thanks to #jamesdlin, the solution is:
VoidCallback callback;
callback?.call();

Related

What should I pass to the .success() for `Result<Void, Error>`? [duplicate]

This question already has answers here:
How to handle Void success case with Result lib (success/failure)
(4 answers)
Closed 3 years ago.
I have a network request, but I don't really care about the success response, so I use a Result<Void, Error> for return value. The problem is when I assign .success() to it, the compiler return the following error:
Missing argument for parameter #1 in call
I have tried passing empty, nil, but neither can pass the compiling.
The code is like this:
var result: Result<Void, Error>?
result = .success()
How can I make it work?
result = .success(()) should work

Initializer for conditional binding must have Optional type, not 'Substring.SubSequence' (aka 'Substring') - swift [duplicate]

This question already has answers here:
Error: Initializer for conditional binding must have optional type, not 'String' [duplicate]
(2 answers)
Closed 4 years ago.
I am working on a little project. to avoid nil value, I am using if condition
if let _result = _fullString.suffix(from: startIndex!){
//do something
}
but it shows error
Initializer for conditional binding must have Optional type, not
'Substring.SubSequence' (aka 'Substring')
Is happens because suffix function not returning any optional value. that's why it cause optional binding is failed.
You can directly use suffix value by using below code
let _result = _fullString.suffix(from: startIndex!)

The contextual keyword 'var' may only appear within a local variable declaration Or In Script Code [duplicate]

This question already has answers here:
Why can't class fields be var? [duplicate]
(4 answers)
Closed 6 years ago.
Hey I'm trying to Define a mock database . I'm encountering an error while trying to equate var to Mock<'Repository'> The error is :
The contextual keyword 'var' may only appear within a local variable declaration Or In Script Code.
The Code that I have written is :
public class MockingDatabse
{
//Mock a Payment Info
var newPayment = new Mock<IPayment>();
}
I know that I can replace 'var' with 'Mock<"Repository">'. But I wanna know I'm not able to use
'var'
Try this:
public class MockingDatabse
{
//Mock a Payment Info
Mock<IPayment> newPayment = new Mock<IPayment>();
}

Dart calling a member function by function name [duplicate]

This question already has answers here:
Dynamic class method invocation in Dart
(3 answers)
Closed 8 years ago.
I am wondering if there is anyway to call a function by its name in dart as in javascript.
I would like to do something as such:
foo["bar"]();
I don't want readers to think what the questioner wants isn't possible in Dart, so I'm adding an answer.
You need to use Mirrors to call a method if you have its name available as a string. Here is an example:
import 'dart:mirrors';
class Foo {
bar() => "bar";
}
void main() {
var foo = new Foo();
var mirror = reflect(foo);
print(mirror.invoke(#bar, []).reflectee); // Prints 'bar'.
}

What is the -> (dash greater than) operator in Ruby/Rails [duplicate]

This question already has answers here:
What is the -> (stab) operator in Ruby? [duplicate]
(1 answer)
What does -> mean in Ruby [duplicate]
(2 answers)
Closed 9 years ago.
I just ran across the following line of code in a Rails app:
scope :for_uid, ->(external_id) { where(external_id: external_id) }
What does the -> operator mean? It's kind of hard to Google.
This is syntactic sugar.
->(external_id) { where(external_id: external_id) }
is equal to:
lambda { |external_id| where(external_id: external_id) }
It's new lambda notation. This syntax was introduced in ruby 1.9, and is used to define unnamed functions.
In your example it is scope defined by unnamed function.
The -> operator was introduced in Ruby 1.9 as a shorthand syntax for the old lambda function. It behaves nearly identically to the lambda function but allows you to specify parameters outside the block:
lambda {|param| puts param }
# becomes
-> (param) { puts params }

Resources