Flutter Null check operator used on a null value - flutter

_stores is giving me issue where if value.stores is null, I get the below null error. how to resolve this issue ? null-safety enabled as this project is 2.12.2 plus.
Code:
dynamic _stores = useProvider(
storeViewModelProvider.select((value) => value.stores));
Code also tried :
dynamic _stores = useProvider(storeViewModelProvider
.select((StoreViewModel? value) => value!.stores));
Error: Null check operator used on a null value
Can someone let me know what is wrong here ?

the attribute receiving useProvider has to be nullable.
after try this:
dynamic _stores = useProvider(
storeViewModelProvider?.select((value) => value?.stores));

Related

"AppLocalizations.of(context)!value" make error "Null check operator used on a null value"

When I am using AppLocalizations.of(context)!.value in ternary operation in flutter , then it show error "Null check operator used on a null value".
Here is an example of usage that makes error:
isLoading ? AppLocalizations.of(context)!.loading : AppLocalizations.of(context)!.value
In this code show error Null check operator used on a null value
isLoading ? Text(AppLocalizations.of(context)!.orderIsBeingSending) : Text("${AppLocalizations.of(context)!.orderId} : ${orderSummary.orderResponse?.no.toString()}");
Debug output:
add this line in l10n.yaml:
nullable-getter: false

flutter null safety from prefs.setStringList -> error : the argument list<String?> can't be assigned to the parameter type List <String>

Hello I try to make null safety migration, but have a probleme with
prefs.setStringList("save_list_autre2",save_list.toList());
the error is : the argument list<String?> can't be assigned to the parameter type List
RegExp regExp = new RegExp(r'(...)');
var save_list= regExp.allMatches(decrypted).map((z) => z.group(0));
if(regExp.allMatches(decrypted)==null ){
[...]
} else {
prefs.setStringList("save_list_autre2",save_list.toList());
}
I add .toString() from save_list= regExp.allMatches(decrypted).map((z) => z.group(0).toString());
the error is removed but I don't know if it's the good solution
When you call z.group(0) the group method could return null if there was no group with the given index (0 in this case). so dart thinks you might have some null values on your list of strings.
The reality is that you shouldn't get any null values because you are passing a 0. So to tell dart that the method isn't returning null, use the ! operator:
regExp.allMatches(decrypted)
.map((z) => z.group(0)!)
If, by some chance, you happened to have any null values, this code will not work (as I said, I believe you won't but what do I know? I've never used regexes with dart)
If that's the case, you first need to filter the null values and then use the !:
regExp.allMatches(decrypted)
.map((z) => z.group(0))
.where((v) => v != null) // filter out null values
.map((v) => v!); // let dart know you don't have any null values.
Hopefully that's enough to fix the problem

Avoid "value can be null" warning for extension method

I am using an extension on Iterable that adds the following two methods
extension MyIterableExt<T> on Iterable<T> {
bool get isEmptyOrNull => this == null || this.isEmpty;
bool get isNotEmptyOrNull => this != null && this.isNotEmpty;
}
This works as I expected, but when I try to use it in statements where the receiver can be null, I get a warning in IntelliJ. For example, the following code works and prints true:
List<String> x;
print('${x?.reversed.isEmptyOrNull}');
Is there any way I can make the Dart Analyzer understand that the extension checks if this == null and thus not show the warning?
Please note that I do not want to have to add suppression directives to every file or line where I use this extension!
I know I can add // ignore_for_file: can_be_null_after_null_aware or // ignore: can_be_null_after_null_aware to make the warning go away, but what I'm looking for is a way to make the Dart Analyzer understand that the warning for this particular extension method is not needed.
This is in a non null-safe project, if that matters.
you can make Iterable Nullable
extension MyIterableExt<T> on Iterable<T>? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
Make extension base on Iterable? is the way, if you have to find something to disable the warning but not want to change the code then im sorry cause i never do that before.
void main() {
Iterable? a = [];
Iterable b = ['1', '2', '3'];
print('a.isEmptyOrNull: ${a.isEmptyOrNull}');
print('b.isEmptyOrNull: ${b.isEmptyOrNull}');
print('a.isNotEmptyOrNull: ${a.isNotEmptyOrNull}');
print('b.isNotEmptyOrNull: ${b.isNotEmptyOrNull}');
}
extension on Iterable? {
bool get isEmptyOrNull => this == null || this!.isEmpty;
bool get isNotEmptyOrNull => this != null && this!.isNotEmpty;
}
result
a.isEmptyOrNull: true
b.isEmptyOrNull: false
a.isNotEmptyOrNull: false
b.isNotEmptyOrNull: true
Based on the Flutter docs, you should be able to add a comment at the top of your code that will essentially disable null safety checks:
// #dart=2.9
import 'src/my_app.dart';
main() {
//...
}
You should be able to put that comment at the top of any specific file you want this behavior. It doesn't have to be in your "main" file.

Confusion with Null safety

I'm pretty new to flutter and because of the null safety feature I've been getting a lot of errors from a code that will run perfectly fine in Java or other langs. For example this-
int ? i;
var icecreamFlavours = ['chocolate', 'vanilla', 'orange'];
icecreamFlavours.forEach((item) {
i++; //This is where I get the error
print('We have the $item flavour');
});
My Error Message
Error: Operator '+' cannot be called on 'int?' because it is potentially null.
i++;
Error: A value of type 'num' can't be assigned to a variable of type 'int?'.
i++;
You can't do either i++ or i+=1 on a nullable variable, because the reason is simple, null variable can have null values which will make increment no sense.
In addition to this, you can't even do i!++ or i!+=1 even if you are sure that i is not null. There is an open issue for this https://github.com/dart-lang/language/issues/1113
So for now you can do this i = i! + 1
Update:
For i = i! + 1 to work you need to be sure about i not being null. So it's better to initialise/assign i with some value before using it.

Flutter how to test if ui.Image is empty or null correctly?

I define globally a variable as ui.Image _img1Global = null as ui.Image; then I wanted to check to determine if the value is null or not.
if (_img1Global != null as ui.Image) {
canvasWrapper.save();
canvasWrapper.drawImage(
_img1Global, Offset(x, y - 2), new Paint());
canvasWrapper.restore();
}
I want to check if it is not null then I want to operate the drawImage but I keep getting error as type 'Null' is not a subtype of type 'Image' in type cast at this if statement ? How to check if its filled with ui.Image and check its correct type?
I have even try this
_img1Global != null as ui.Image
? print("is null")
: print("is not null");
still I am getting same error ?
null as ui.Image
It is throwing an error because the above code is telling that set null type as an Image type
To check the type of a variable use the runtimeType operator
if (_img1Global.runtimeType == Image) print('True')