0

I'm building a Flutter app with the null safety. I have a problem when I want to give a nullable variable to a function which can't accept a nullable one. I tried to check if it was null before calling the function but it doesn't work.

if (accountsFactory.selected != null){
  _accountBloc?.select.add(accountsFactory.selected);
}

In this sample accountFactory.selected is the nullable one (Account?) and _accountBloc?.select is a StreamSink<Account>.

Does anyone know how I can make this work ? I would like to keep <Account> on my stream if possible.

Haka
  • 17
  • 5

1 Answers1

0

I think what you need is the null assertion operator !, you would want to do something like this:

if (accountsFactory.selected != null){
  _accountBloc?.select.add(accountsFactory.selected!); // assert that selected is not null
}

more info about null assertion here

if this what you're looking for ?

Michael Soliman
  • 2,240
  • 3
  • 7
  • 20
  • Yes It is ! thanks a lot. I'm pretty new with the null-safety on Flutter and I didn't know this operator. – Haka Feb 24 '22 at 16:49
  • https://github.com/michaelsoliman1/intro_to_dart, check this repo, it has a brief and easy introduction to null safety, also you may accept the answer if it helped! – Michael Soliman Feb 24 '22 at 16:52
  • thank you, I could not accept it due to a time restriction. It's done now. Thank you again. – Haka Feb 24 '22 at 16:56