Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - Flutter Error: A non-null value must be returned since the return type 'Widget' doesn't allow null

Launching libmain.dart on F8331 in debug mode... Running Gradle task 'assembleDebug'... ../../Documents/Android%20Studio%20Project/kotaawan/lib/feature/main/home/product/new-product.dart:37:22: Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.

  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/E:/flutter/packages/flutter/lib/src/widgets/framework.dart'). builder: (BuildContext context, AsyncSnapshot snapshot) { ^ ../../Documents/Android%20Studio%20Project/kotaawan/lib/feature/main/home/product/new-product.dart:22:19: Error: Field 'futureProduct' should be initialized because its type 'Future' doesn't allow null.
  • 'Future' is from 'dart:async'.
  • 'Product' is from 'package:kotaawan/model/product.dart' ('lib/model/product.dart'). Future futureProduct; ^^^^^^^^^^^^^

FAILURE: Build failed with an exception.

  • Where: Script 'E:flutterpackagesflutter_toolsgradleflutter.gradle' line: 991

  • What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.

Process 'command 'E:flutterinflutter.bat'' finished with non-zero exit value 1

  • Try: Run with --stacktrace 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 3m 20s Exception: Gradle task assembleDebug failed with exit code 1

question from:https://stackoverflow.com/questions/65932691/flutter-error-a-non-null-value-must-be-returned-since-the-return-type-widget

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Running Gradle task 'assembleDebug'... ../../Documents/Android%20Studio%20Project/kotaawan/lib/feature/main/home/product/new-product.dart:37:22: Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.

From these lines, it seems like you are defining a function at line 37 that supposed to return a Widget, but instead return null. Keep in mind that when placing a widget within a screen in Flutter, that widget must not be null. You can catch the null widget with a blank Container like:

...
child: someWidget ?? Container()
...

Small note: When asking question, you should include the block of code mentioned in the error in your question (The block from line 37 in your new-product.dart file), so that others can pinpoint the error faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...