Why does it break in Foo when passing an instance of D2 rather than setting d1 to null? In .NET, it's advisable to use as instead of is because is normally requires two cast/conversion which is more expensive than null testing.
class Base {}
class D1 extends Base{}
class D2 extends Base{}
void Foo(Base d1OrD2) {
var d1 = d1OrD2 as D1;
if (d1 != null) print("it's d1");
else {
var d2 = d1OrD2 as D2;
if (d2 != null) print("it's d2");
}
}
void main() async{
Foo(D2()); // throws exception D2 is not an instance of D1.
Foo(D1()); // works
}
Because the as operator in Dart throws if the object is not an instance of the type on the right (or null). It is not similar to the as operator in C# which evaluates to null instead of throwing.
Your code starts with var d1 = d1OrD2 as D1; When d1OrD2 is a D2 instance, that operation throws.
What you should do instead is:
void Foo(Base d1OrD2) {
if (d1OrD2 is D1) {
// Static type of d1OrD2 is D1 here, you can call D1 methods on it.
print("it's d1");
} else {
var d2 = d1OrD2 as D2;
print("it's d2");
}
}
Related
void foo<T extends num, String> (T t) {
if (t is String) {
String s = t; // Error
}
}
A value of type 'T' can't be assigned to a variable of type 'String'.
You won't be able to do this with base Dart as your generic type T can only extends one class.
The only way I would see such a behavior feasible would be by using a 3rd party packages such as dartz with its Either type.
Example
void foo<T extends num>(Either<T, String> t) {
final String s;
if (t.isRight()) {
s = (t as Right<T, String>).value;
} else {
s = (t as Left<T, String>).value.toStringAsFixed(3);
}
print(s);
}
foo(Left(1.0)); // prints '1.000'
foo<int>(Right('bar')); // prints 'bar'
There is no syntax to specify that a generic type implement multiple interfaces, so there is no way for this to work with compile-time checks.
Furthermore, your particular example can't work because num and String cannot be extended nor implemented, so it's impossible to have a type that implements both.
If we change your example, which relies on a runtime check, to use two custom types, it still won't work:
class C1 {}
class C2 {
void f() => print('C2.f');
}
class C3 implements C1, C2 {
#override
void f() => print('C3.f');
}
void foo<T extends C1>(T t) {
if (t is C2) {
t.f(); // 'f' isn't defined for the type <unknown>
}
}
See https://github.com/dart-lang/language/issues/2047: t isn't related to C2, so the is C2 check unfortunately will not automatically promote it to C2. You instead can use a runtime cast:
void foo<T extends C1>(T t) {
if (t is C2) {
(t as C2).f();
}
}
or upcast to Object/dynamic first:
void foo<T extends C1>(T t) {
Object t0 = t;
if (t0 is C2) {
t0.f();
}
}
But really you should just use T extends C3 if possible.
This question already has answers here:
"The operator can’t be unconditionally invoked because the receiver can be null" error after migrating to Dart null-safety
(3 answers)
Closed 12 months ago.
I have migrated my Dart code to NNBD / Null Safety. Some of it looks like this:
class Foo {
String? _a;
void foo() {
if (_a != null) {
_a += 'a';
}
}
}
class Bar {
Bar() {
_a = 'a';
}
String _a;
}
This causes two analysis errors. For _a += 'a';:
An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
For Bar() {:
Non-nullable instance field '_a' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
In both cases I have already done exactly what the error suggests! What's up with that?
I'm using Dart 2.12.0-133.2.beta (Tue Dec 15).
Edit: I found this page which says:
The analyzer can’t model the flow of your whole application, so it can’t predict the values of global variables or class fields.
But that doesn't make sense to me - there's only one possible flow control path from if (_a != null) to _a += 'a'; in this case - there's no async code and Dart is single-threaded - so it doesn't matter that _a isn't local.
And the error message for Bar() explicitly states the possibility of initialising the field in the constructor.
The problem is that class fields can be overridden even if it is marked as final. The following example illustrates the problem:
class A {
final String? text = 'hello';
String? getText() {
if (text != null) {
return text;
} else {
return 'WAS NULL!';
}
}
}
class B extends A {
bool first = true;
#override
String? get text {
if (first) {
first = false;
return 'world';
} else {
return null;
}
}
}
void main() {
print(A().getText()); // hello
print(B().getText()); // null
}
The B class overrides the text final field so it returns a value the first time it is asked but returns null after this. You cannot write your A class in such a way that you can prevent this form of overrides from being allowed.
So we cannot change the return value of getText from String? to String even if it looks like we checks the text field for null before returning it.
An expression whose value can be 'null' must be null-checked before it can be dereferenced. Try checking that the value isn't 'null' before dereferencing it.
It seems like this really does only work for local variables. This code has no errors:
class Foo {
String? _a;
void foo() {
final a = _a;
if (a != null) {
a += 'a';
_a = a;
}
}
}
It kind of sucks though. My code is now filled with code that just copies class members to local variables and back again. :-/
Non-nullable instance field '_a' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
Ah so it turns out a "field initializer" is actually like this:
class Bar {
Bar() : _a = 'a';
String _a;
}
There are few ways to deal with this situation. I've given a detailed answer here so I'm only writing the solutions from it:
Use local variable (Recommended)
void foo() {
var a = this.a; // <-- Local variable
if (a != null) {
a += 'a';
this.a = a;
}
}
Use ??
void foo() {
var a = (this.a ?? '') + 'a';
this.a = a;
}
Use Bang operator (!)
You should only use this solution when you're 100% sure that the variable (a) is not null at the time you're using it.
void foo() {
a = a! + 'a'; // <-- Bang operator
}
To answer your second question:
Non-nullable fields should always be initialized. There are generally three ways of initializing them:
In the declaration:
class Bar {
String a = 'a';
}
In the initializing formal
class Bar {
String a;
Bar({required this.a});
}
In the initializer list:
class Bar {
String a;
Bar(String b) : a = b;
}
You can create your classes in null-safety like this
class JobDoc {
File? docCam1;
File? docCam2;
File? docBarcode;
File? docSignature;
JobDoc({this.docCam1, this.docCam2, this.docBarcode, this.docSignature});
JobDoc.fromJson(Map<String, dynamic> json) {
docCam1 = json['docCam1'] ?? null;
docCam2 = json['docCam2'] ?? null;
docBarcode = json['docBarcode'] ?? null;
docSignature = json['docSignature'] ?? null;
}
}
I have a class (with more properties than the example, of course).
How to write the following simple test? I know that equality in Dart is by object instance. How to compare the full object state? I tested with the same matcher as well, with no luck.
import 'package:test/test.dart';
class UnderTesting {
var a;
var b;
UnderTesting({this.a, this.b});
}
void main() {
test("compare objects", () {
final obj1 = UnderTesting(a:1, b:2);
final obj2 = UnderTesting(a:1, b:2);
// Next will fail because it is checking if it is the same instance
expect(obj1, equals(obj2));
} );
}
You need to override the == operator (and should therefore also override hashCode) for UnderTesting. The documentation for equals tells us how to does test for equality:
If [expected] is a [Matcher], then it matches using that. Otherwise it tests for equality using == on the expected value.
So you code should be something like this:
import 'package:test/test.dart';
import 'package:quiver/core.dart';
class UnderTesting {
int a;
int b;
UnderTesting({this.a, this.b});
#override
bool operator ==(Object other) =>
(other is UnderTesting) ? (a == other.a && b == other.b) : false;
#override
int get hashCode => hash2(a, b);
}
void main() {
test("compare objects", () {
final obj1 = UnderTesting(a: 1, b: 2);
final obj2 = UnderTesting(a: 1, b: 2);
expect(obj1, equals(obj2)); // All tests passed!
});
}
I can recommend the quiver package for making easy hashCode implementations: https://pub.dev/documentation/quiver/latest/quiver.core/quiver.core-library.html
Context:
In Ax, I'm exposing a webservice. The method takes 3 parameters. Two of them should be Nullable<> but I fail to do that in X++.
In c# the code would look like:
private static void FooBar(string[] things, DateTime? d1, DateTime? d2)
{
if (d1 == null || d2 == null)
{
//Do Something
}
else {
//Something Else
}
}
How could I translate this? How can I build a method with build optional, nullable parameters?
With Method overload not suported in X++.
This should translate to the following:
private static void FooBar(str things[], utcdatetime d1 = DateTimeUtil::minValue(), utcdatetime d2 = DateTimeUtil::minValue())
{
if(d1!=DateTimeUtil::minValue()||d2!=DateTimeUtil::minValue())
{
//do something
}
else
{
//something else
}
}
things is mandatory, d1 & d2 are optional
On Dart 1.0.0, I just tried:
class MyClass {
int x;
bool b;
MyClass(int x, [bool b = true]) {
if(?b) {
// ...
}
}
}
And am getting a compiler error on the ?b part:
The argument definition test ('?' operator) has been deprecated
So what's the "new" way of testing for whether or not an argument was supplied?
There is no way to test if an argument was provided or not. The main-reason for its removal was, that it was very complex to forward calls this way.
The generally preferred way is to use null as "not given". This doesn't always work (for example if null is a valid value), and won't catch bad arguments. If null is used, then the parameter must not have a default-value. Otherwise the parameter is not null but takes the default-value:
foo([x = true, y]) => print("$x, $y");
foo(); // prints "true, null"
So in your case you should probably do:
class MyClass {
int x;
bool b;
MyClass(int x, [bool b]) {
if(b == null) { // treat as if not given.
// ...
}
}
}
This makes new MyClass(5, null) and new MyClass(5) identical. If you really need to catch the first case, you have to work around the type-system:
class _Sentinel { const _Sentinel(); }
...
MyClass(int x, [b = const _Sentinel()]) {
if (b == const _Sentinel()) b = true;
...
}
This way you can check if an argument has been provided. In return you lose the type on b.
The argument definition test operator was deprecated because it was redundant with checking for null; an optional parameter that was omitted would get the value null, and the caller could've passed null explicitly anyway. So instead use == null:
class MyClass {
int x;
bool b;
MyClass(int x, [bool b]) {
if (b == null) {
// throw exception or assign default value for b
}
}
}