TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
return statement is a control flow construct that immediately terminates the execution of the current function and passes control, along with an optional evaluated expression, back to the calling context.
Syntax
Mechanics and Compiler Rules
Type Conformance In standard synchronous functions, the evaluated type of theexpression must be statically assignable to the declared return type of the enclosing function. Under Dart’s sound type system, returning a type that does not conform to the function signature results in a compile-time error.
async modifier, the return statement automatically wraps the evaluated expression in a Future. Because of this, the standard static assignability rule is relaxed: if an async function has a declared return type of Future<T>, the return expression can be of type T. Dart will automatically wrap the value T into a completed Future<T>. Furthermore, if the returned expression is itself a Future<T>, Dart flattens the return value to prevent nested futures (e.g., avoiding Future<Future<T>>).
void type indicate that the caller should ignore the return value. Within a void function, an empty return; statement can be used to terminate execution. A return statement in a void function can also be followed by an expression, provided the expression’s static type is void or Never. Returning any other type (including dynamic) from a void function results in a compile-time error.
return statement causes the function to implicitly return null. However, the compiler strictly restricts which functions are allowed to complete normally. If a function reaches the end of its block but its signature does not permit it, the compiler emits a body_might_complete_normally (or body_might_complete_normally_nullable) error.
A function is allowed to complete normally without an explicit return only if:
- It is a standard synchronous function with a declared return type of
void,dynamic,FutureOr<void>, orFutureOr<dynamic>. - It is an
asyncfunction with a declared return type ofvoid,dynamic,Future<void>,Future<dynamic>,FutureOr<void>, orFutureOr<dynamic>. - It is a generator function (
sync*orasync*), regardless of its declared return type (e.g.,Iterable<int>orStream<String>), because reaching the end simply terminates the generator.
Null (or Future<Null>) are not permitted to complete normally and must explicitly return null;.
=> (fat arrow) operator as a lexical shorthand for functions containing a single return statement. The expression following the arrow is automatically evaluated and returned.
sync* for Iterable and async* for Stream), the return statement does not emit a value to the sequence. Instead, it immediately terminates the generator, closing the underlying iterator or stream. Values in these functions must be emitted using yield or yield*, while return is strictly used for early termination.
Master Dart with Deep Grasping Methodology!Learn More





