How to create null safe block in flutter? - flutter

How do I null check or create a null safe block in Flutter?
Here is an example:
class Dog {
final List<String>? breeds;
Dog(this.breeds);
}
void handleDog(Dog dog) {
printBreeds(dog.breeds); //Error: The argument type 'List<String>?' can't be assigned to the parameter type 'List<String>'.
}
void printBreeds(List<String> breeds) {
breeds.forEach((breed) {
print(breed);
});
}
If you try to surround it with an if case you get the same error:
void handleDog(Dog dog){
if(dog.breeds != null) {
printBreeds(dog.breeds); //Error: The argument type 'List<String>?' can't be assigned to the parameter type 'List<String>'.
}
}
If you create a new property and then null check it it works, but it becomes bothersome to create new properties each time you want to null check:
void handleDog(Dog dog) {
final List<String>? breeds = dog.breeds;
if (breeds != null) {
printBreeds(breeds); // OK!
}
}
Is there a better way to do this?
Like the ?.let{} syntax in kotlin?

To get something similar to Kotlins .let{} i created the following generic extension :
extension NullSafeBlock<T> on T? {
void let(Function(T it) runnable) {
final instance = this;
if (instance != null) {
runnable(instance);
}
}
}
And it can be used like this:
void handleDog(Dog dog) {
dog.breeds?.let((it) => printBreeds(it));
}
"it" inside the let function will never be null at runtime.
Thanks to all the suggestions, but they were all some variation of moving the null check further down the code execution cain, which was not what i was looking for.

Yes, you'll have create a local variable just like you did to handle those things because if you don't create a local variable then if there is a class which is extending the Dog class can override breeds which will then become nullable even after you had checked it in the first place.
The other solution you can try is changing the List<String> to nullable in printBreeds method.
void handleDog(Dog dog) {
printBreeds(dog.breeds);
}
void printBreeds(List<String>? breeds) {
breeds?.forEach((breed) {
print(breed);
});
}

This error is right //Error: The argument type 'List<String>?' can't be assigned to the parameter type 'List<String>'.
as null type list is passing to function which says it accepts a non-null list
By below way breeds can be accessible
void printBreeds(List<String>? breeds) {
breeds?.forEach((breed) {
print(breed);
});
}
Also, if we don't want nullable operation every time, we can handle it while calling
Example:
class Dog {
final List<String>? breeds;
Dog(this.breeds);
}
void handleDog(Dog dog) {
print("handleDog");
printBreeds(dog.breeds!);
}
// This method only called if breeds not null
void printBreeds(List<String> breeds) {
print("printBreeds");
breeds.forEach((breed) {
print(breed);
});
}
void main() {
var dog = Dog(null);
handleDog(dog);
}
Output:
printBreeds

Related

Avoid duplication of null type checking in Dart

My current goal is to remove this code duplication:
final int? myNullableInt = 10;
/// Everywhere I need to do this null verification:
if (myNullableInt == null) return null;
return someOtherMethodThatReceivesANonNullableInt(myNullableInt);
I want to convert to something like we have in Kotlin:
final int? myNullableInt = 10;
return myNullableInt?.apply((myInt) => someOtherMethodThatReceivesANonNullableInt(myInt));
I did it:
extension ApplyIfNotNull on Object? {
T? apply<T extends Object?>(Object? obj, T? Function(Object) fn) {
if (obj == null) return null;
return fn(obj);
}
}
But this gives me a static error:
The argument type 'Object' can't be assigned to the parameter type 'int'.
Note: this should work with all types, e.g ints, Strings, double and MyOwnClassTypes.
Is there something I can do? or am I missing something?
extension ApplyIfNotNull on Object? {
T? apply<T extends Object?>(Object? obj, T? Function(Object) fn) {
if (obj == null) return null;
return fn(obj);
}
}
That doesn't work because it declares that the callback be capable of accepting any Object argument, but you're presumably trying to use it with a function that accepts only an int argument. It's also unclear why you've made an extension method since it doesn't involve the receiver (this) at all.
You need to make your function generic on the callback's argument type as well:
R? applyIfNotNull<R, T>(T? obj, R Function(T) f) =>
(obj == null) ? null : f(obj);
(That's the same as what I suggested in https://github.com/dart-lang/language/issues/360#issuecomment-502423488 but with the arguments reversed.)
Or, as an extension method, so that it can work on this instead of having the extra obj argument:
extension ApplyIfNotNull<T> on T? {
R? apply<R>(R Function(T) f) {
// Local variable to allow automatic type promotion. Also see:
// <https://github.com/dart-lang/language/issues/1397>
var self = this;
return (self == null) ? null : f(self);
}
}
Also see https://github.com/dart-lang/language/issues/360 for the existing language feature request and for some other suggested workarounds in the meantime.

Flutter/Dart: Why does this statement effect null-nafety?

I realized this is just fine:
List<int> list = [];
int? b;
void addBarIfNull() {
if (b != null) {
list.add(b); // no problem
}
}
But adding this one statement b = null after adding b to the list, lets the linter complain with: The argument type 'int?' can't be assigned to the parameter type 'int':
List<int> list = [];
int? b;
void addBarIfNull() {
if (b != null) {
list.add(b); // problem: The argument type 'int?' can't be assigned to the parameter type 'int'
b = null;
}
}
Can someone explain what is going on here? Why is b considered as int? in list.add(b) if we clearly check before that it is not null? Why do both code snippets differ in handling this?
It is null safey issue when you declare
{
int? b;
}
you allow b to be null or have value but when you try to add
{
list.add(b);
}
b to the list you don't allow the list to contain null value that why the error message said you can't add int? to int you need to allow the list to contain null value if that whta you needed.
{
List<int?> list =[];
}

Why does the dart "is" operator behave differently for local variables vs. class fields? [duplicate]

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;
}
}

Dart - A value of type 'bool' can't be assigned to a variable of type 'bool'

Pasting the following into dartPad.dev gives the error for the assignment classtwoInstance.fieldOne = arg;
Error: A value of type 'bool' can't be assigned to a variable of type 'bool'
Casting to a bool also gives an 'unnecessary cast' message
How do I assign the bool argument arg to fieldOne?
class ClassOne {
Function<T>(T) setFunction;
ClassOne({this.setFunction});
}
class ClassTwo {
bool fieldOne;
}
testFunction<T>(T value){
ClassTwo classtwoInstance = ClassTwo();
ClassOne classOneInstance = ClassOne(setFunction: <bool>(bool arg) {
classtwoInstance.fieldOne = arg; // Error A value of type 'bool' can't be assigned to a variable of type 'bool'
return null;
});
return classOneInstance;
}
void main() {
ClassOne classWithFunction = testFunction<bool>(true);
classWithFunction.setFunction(true);
}
Since you're using a generic type on your Function<T>(T) setFunction, the compiler gets confused when it's set to as bool in testFunction(). What you can do here is change the data type to generic and then cast arg with bool when setting to cassTwoInstance.fieldOne
Here's the updated code that you can try on dartpad.dev
class ClassOne {
Function<T>(T) setFunction;
ClassOne({required this.setFunction});
}
class ClassTwo {
bool? fieldOne;
}
testFunction<T>(T value) {
ClassTwo cassTwoInstance = ClassTwo();
ClassOne classOneInstance = ClassOne(setFunction: <T>(T arg) {
cassTwoInstance.fieldOne = arg as bool;
print('ClassTwo fieldOne: ${cassTwoInstance.fieldOne}');
return null;
});
return classOneInstance;
}
void main() {
ClassOne classWithFunction = testFunction<bool>(false);
classWithFunction.setFunction(false);
}

Flutter: create an Object from Type or String

I want to create an Object from an object type defined in Type or from an object name defined in String. The following example uses a String to hold the object type. But I think this is not that elegant - even with Type, this if block would increase dramatically for a lot of object types...
I didn't find a better solution for this yet. How can I create that object dynamically from the specified object type?
if (model == 'Event') {
data = Event.fromMap(result);
}
else if (model == 'Content') {
data = Content.fromMap(result);
}
else if (...) {
// ...
}
This is other approach.
class Event{
Event.fromMap(_map){
print('This is an event');
print(_map);
}
}
class Content{
Content.fromMap(_map){
print('This is a content');
print(_map);
}
}
Map<String, Function> types = {
'Event' : (_map)=>Event.fromMap(_map),
'Content' : (_map)=>Content.fromMap(_map),
};
void main() {
var a = types['Event']({'test':'success_event'});
print(a.runtimeType);
var b = types['Content']({'test':'success_content'});
print(b.runtimeType);
}
Its a bit more scalable (since only depends on add the class constructor into the map).
The explanation:
class Event{
Event.fromMap(_map){
print('This is an event');
print(_map);
}
}
class Content{
Content.fromMap(_map){
print('This is a content');
print(_map);
}
}
Here we are creating the test classes. nothing important.
Map<String, Function> types = {
'Event' : (_map)=>Event.fromMap(_map),
'Content' : (_map)=>Content.fromMap(_map),
};
Here we are defining a Map. Why? Because it allows us to access some value through some key in constant time. In this case, the keys are the Strings 'Event', 'Content', but also can be types as you wanted. For simplicity, let them be Strings. The values are Function's, in this example only getting as parameter a _map (because the Class constructors in the example require one parameter _map). So, if you need more types, only add the type and the function encapsulating the constructor for that type.
void main() {
var a = types['Event']({'test':'success_event'});
print(a.runtimeType);
var b = types['Content']({'test':'success_content'});
print(b.runtimeType);
}
Finally you can instantiate the classes easily. Only with the type string and passing to the function the values you want (In this example a map with a key 'test').
In your example would be something like:
data = types[model](result);