A non-null value must be returned from async function

A value of type 'Null' can't be returned from the function 'fetchData' because it has a return type of 'Future<String>'.

Quick Answer

An async function with a non-nullable return type does not return a value on all code paths.

Why This Happens

In Dart with null safety, an async function declared to return Future<String> must return a String on every code path. If any path returns null or has no return statement, the compiler raises this error. Add return statements or make the return type nullable.

The Problem

Future<String> fetchData() async {
  try {
    return await api.getData();
  } catch (e) {
    print(e);
    // Missing return!
  }
}

The Fix

Future<String> fetchData() async {
  try {
    return await api.getData();
  } catch (e) {
    print(e);
    return ''; // Or rethrow the exception
  }
}

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the compiler error about a non-null return type. This means a code path does not return a value.

  2. 2

    Find the cause

    Check all code paths in the function, especially catch blocks and conditional branches, for missing return statements.

  3. 3

    Apply the fix

    Add return statements to all code paths, change the return type to nullable (Future<String?>), or rethrow exceptions.

Got the actual stack trace?

Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.

Explain my error

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free