Home > other >  Method 'replaceFirst' cannot be called on 'String?'
Method 'replaceFirst' cannot be called on 'String?'

Time:11-15

Error: Method 'replaceFirst' cannot be called on 'String?' because it is potentially null. Try calling using ?. instead.)

.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));

 

CodePudding user response:

As Saffron-codes says, you cant do a replaceFirst on a 'String?' variable since it can be null, and dart is null safe.

There's two options for you to do. You can either make the variable a 'String' instead, if you do this you'll have to give it a value when initiating it:

String variableName = ''

Instead of:

String? variableName

You could also do a null-check (adding a questionmark before .replaceFirst) when calling replaceFirst, this is what it suggests doing in the error message 'Try calling using ?. instead.':

variableName?.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));

CodePudding user response:

Add the ! operator before . to make it non-nullable

!.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
  • Related