Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get rid of the nullable mark (?) on my variable in Flutter

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading