Non-void function returns nothing dart

Morez

I have a function that returns int value but in some circumstances I want to terminate the function and return nothing.

Just to show an example:

int numberFunc(int num){
    if(num > 10){
       return; 
/* terminates the function for numbers more than 10 */
     }
    return num;
}

This works fine but I get a warning that says the function has a return type of int but it doesn’t finish with a return statement.

Will there be a problem if I use something like this and what can be a solution for that?

Thanks

Solution:

As julemand101 explained, We can return a null value. Also the code above returns a null value for numbers more than 10 so we have to take care of the possible null values later on.

julemand101

What you properly want is to return null to indicate that the method are not returning any value:

int numberFunc(int num){
    if(num > 10){
       return null; 
/* terminates the function for numbers more than 10 */
     }
    return num;
}

But remember that the method which are using the result from numberFunc needs to be aware that the returned value can be null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

void function returns nothing - C

In a non-void function I want to return nothing

python function returns nothing

Return function returns nothing

dart: void function for ffi

Why can't I annotate a function expression as void despite that it returns nothing in TypeScript?

C++ How to make a non void function return nothing for if else statements?

How to create function that returns nothing

Why XMPP function returns nothing?

Laravel hasmany function returns nothing

Calling a stored function that returns nothing

unit test for a function that returns nothing

Function that only returns false returns nothing

Return statement that calls a function that returns void in a function that returns void

unexpected non void return value in void function

Dart Function always returns a Future

Is it possible to return nothing with a non-void method if a certain condition is not met

importing and calling a function in Typsescript which returns nothing

ANY function returns nothing when the database is empty

Replace function $$find always returns nothing

Callback function for database query returns nothing

Google spreadsheet custom function returns nothing

Async function to get JSON returns nothing

if function in javascript returns nothing..why?

For loop returns nothing when using function

What does void Function() do in Dart?

Return Future<void> from a sync function in Dart

Handling errors in void Dart function marked async

find in mongoose returns nothing when there is a non existing field in the schema