Home > other >  Im facing an error FAILURE: Build failed with an exception
Im facing an error FAILURE: Build failed with an exception

Time:07-08

I'm having this error:

-/C:/src/flutter/flutter/packages/flutter/lib/src/services/asset_bundle.dart:240:12: Error: A value of type 'ByteData?' can't be returned from an async function with return type 'Future<ByteData>' because 'ByteData?' is nullable and 'Future<ByteData>' isn't.
 - 'ByteData' is from 'dart:typed_data'.
 - 'Future' is from 'dart:async'.
    return asset;
           ^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1156

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
> Run with --stack trace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 48s
Exception: Gradle task assembly debug failed with exit code 1

CodePudding user response:

This is a nullability problem.
In your case, a function is trying to return a Future<ByteData?> while it should return a Future<ByteData>. The question mark is significant. It indicates that ByteData? may be null, while ByteData may not. If the context already gives you certainty that the value you return is not null, you can just correct the return statement to

...
return result!;

The exclamation mark ensures that the resulting value is not null, by throwing an error at runtime if it is null.
Of course, if the result of this function may be null, it would be more appropriate to rewrite it's signature to Future<ByteData?>

  • Related