I have a method with optional parameters
void method1(String text,
{String? text2}) {
method2(text, text2: text2!);
}
Method1 calls another method with optional parameters
void method2(String text,
{String? text2 = 'helloWorld'}) {
print(text);
print(text2!);
}
when I pass a null value into text2 when I call method1, I have an error CastError (Null check operator used on a null value) when method1 try to call method2
Should I add a condition in method1 like :
void method1(String text,
{String? text2}) {
if(text2 == null)
{
method2(text);
}
else
{
method2(text, text2: text2!);
}
}
or something easier.
Context: flutter 2.10.4
Thanks
CodePudding user response:
It's easier to use ternary operation like:
void method1(String text, {String? text2}) {
text2 != null ? method2(text, text2: text2) : method2(text);
}