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

Issue

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.

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 ?

Answered By – Michael Soliman

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.