Home > database >  The return type 'int' isn't a 'String?', as required by the closure's
The return type 'int' isn't a 'String?', as required by the closure's

Time:12-08

what is the problem when I convert the string type value into an int so it will give me the following error mentioned in question.

CodePudding user response:

If I understand your question correctly, you need to turn a String to int? To do that, use int.parse(yourString) -> you will get int

If you are getting error with int and int? or String and String? (the same type but with question mark), try to use ?? operant

For example (with string value):

MyObject(
  stringField: myString ?? '', //you are ensuring that if your String is null, there will be no error
);

CodePudding user response:

if you want to return a String type data from int data, you need to convert it.

String test() {
    int num = 0;
    return(num.toString());
  }

But if you want to return a int type data from String data, you need to parse it first to int.

int test() {
String pi = '3';
int now = int.parse(pi);  // result from change String to int
return now;

}

  • Related