Setting the scene:
I am trying to set up a flutter example project, which is generated via VSCode (also tried Android Studio with same result). I installed the flutter plugin, dart plugin seems also to be installed correctly and the project itself does also show no errors (was generated via the provided flutter generator in VSCode). I didn't make any changes to the code itself.
Problem:
Now, when I try to build the project in debug mode I am getting many of the above mentioned "cannot be assigned because ... is nullable and ... isn't" errors and the build fails. As far as I can see, many packages are involved.
Flutter doctor tells me, I am on the latest stable version 3.22.1 and up to date. Dart is on 3.4.1, DevTools on 2.34.3. All other doctor checks come back ok except "Visual Studio", which I am not using.
As there are so many "nullable" errors, I am guessing there is a more generic problem underlying, but am not able to figure out how to solve it. Will provide excerpt from console log below.
What did I try to solve the issue:
Just tried to build the project directly after generating it. After first tries didn't work, uninstalled VSCode and Android Studio and reinstalled. Both are, as far as I can tell, the latest stable versions one can get from the respective websites. After that I made sure, that flutter is on the stable channel via "flutter channel stable" and "flutter upgrade".
Additionally I tried this, but even then the project will not build:
flutter pub upgrade --major-versions
Resolving dependencies...
Downloading packages...
> flutter_lints 4.0.0 (was 3.0.2)
leak_tracker 10.0.4 (10.0.5 available)
leak_tracker_flutter_testing 3.0.3 (3.0.5 available)
> lints 4.0.0 (was 3.0.0)
material_color_utilities 0.8.0 (0.11.1 available)
meta 1.12.0 (1.15.0 available)
test_api 0.7.0 (0.7.1 available)
vm_service 14.2.1 (14.2.3 available)
Changed 2 dependencies!
6 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Changed 1 constraint in pubspec.yaml:
flutter_lints: ^3.0.0 -> ^4.0.0
Please help ;-)
Frankly I don't know what else to do. The example project code is not generated or written by me. I would have guessed, that it should run "out of the box".
Could someone help me and guide me in the right direction? Haven't been coding for many years and this is also my first mobile-app-project.
Thanks in advance!
Best regards, Sebastian
Here is part of the error-log (as it is too long for complete post):
Launching lib\main.dart on Android SDK built for x86 in debug mode...
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:343:82: Error: A value of type 'Animation<double>?' can't be assigned to a variable of type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
final Animation<double> currentTrain = (current is TrainHoppingAnimation ? current.currentTrain : current);
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:344:55: Error: A value of type 'Animation<double>?' can't be assigned to a variable of type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
final Animation<double> nextTrain = nextRoute._animation;
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:1087:48: Error: The argument type 'Animation<double>?' can't be assigned to the parameter type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
widget.route.animation,
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:1088:48: Error: The argument type 'Animation<double>?' can't be assigned to the parameter type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
widget.route.secondaryAnimation,
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:1061:44: Error: The argument type 'Animation<double>?' can't be assigned to the parameter type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
widget.route.animation,
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:1062:44: Error: The argument type 'Animation<double>?' can't be assigned to the parameter type 'Animation<double>' because 'Animation<double>?' is nullable and 'Animation<double>' isn't.
- 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../../../flutter/packages/flutter/lib/src/animation/animation.dart').
widget.route.secondaryAnimation,
^
../../../flutter/packages/flutter/lib/src/widgets/routes.dart:1934:17: Error: The argument type 'ImageFilter?' can't be assigned to the parameter type 'ImageFilter' because 'ImageFilter?' is nullable and 'ImageFilter' isn't.
- 'ImageFilter' is from 'dart:ui'.
filter: filter,
^
../../../flutter/packages/flutter/lib/src/rendering/object.dart:3487:38: Error: The argument type 'SemanticsConfiguration?' can't be assigned to the parameter type 'SemanticsConfiguration' because 'SemanticsConfiguration?' is nullable and 'SemanticsConfiguration' isn't.
- 'SemanticsConfiguration' is from 'package:flutter/src/semantics/semantics.dart' ('../../../flutter/packages/flutter/lib/src/semantics/semantics.dart').
describeSemanticsConfiguration(_cachedSemanticsConfiguration);
^
../../../flutter/packages/flutter/lib/src/rendering/object.dart:3687:44: Error: The argument type 'SemanticsConfiguration?' can't be assigned to the parameter type 'SemanticsConfiguration' because 'SemanticsConfiguration?' is nullable and 'SemanticsConfiguration' isn't.
- 'SemanticsConfiguration' is from 'package:flutter/src/semantics/semantics.dart' ('../../../flutter/packages/flutter/lib/src/semantics/semantics.dart').
childConfigurations.add(fragment.config);
^
../../../flutter/packages/flutter/lib/src/rendering/object.dart:4578:31: Context: 'config' refers to a public property so it couldn't be promoted.
See http://dart.dev/go/non-promo-public-field
SemanticsConfiguration? get config;
^
../../../flutter/packages/flutter/lib/src/rendering/object.dart:4658:38: Error: A value of type 'SemanticsNode?' can't be assigned to a variable of type 'SemanticsNode' because 'SemanticsNode?' is nullable and 'SemanticsNode' isn't.
- 'SemanticsNode' is from 'package:flutter/src/semantics/semantics.dart' ('../../../flutter/packages/flutter/lib/src/semantics/semantics.dart').
final SemanticsNode node = owner._semantics;
^
../../../flutter/packages/flutter/lib/src/painting/text_style.dart:1272:49: Error: A value of type 'Color?' can't be assigned to a variable of type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
? a.foreground ?? (Paint()..color = a.color)
^
../../../flutter/packages/flutter/lib/src/painting/text_style.dart:1273:49: Error: A value of type 'Color?' can't be assigned to a variable of type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
: b.foreground ?? (Paint()..color = b.color)
^
../../../flutter/packages/flutter/lib/src/painting/text_style.dart:1277:49: Error: A value of type 'Color?' can't be assigned to a variable of type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
? a.background ?? (Paint()..color = a.backgroundColor)
^
../../../flutter/packages/flutter/lib/src/painting/text_style.dart:1278:49: Error: A value of type 'Color?' can't be assigned to a variable of type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
: b.background ?? (Paint()..color = b.backgroundColor)
^
../../../flutter/packages/flutter/lib/src/painting/text_style.dart:1660:30: Error: The argument type 'FontVariation?' can't be assigned to the parameter type 'FontVariation' because 'FontVariation?' is nullable and 'FontVariation' isn't.
- 'FontVariation' is from 'dart:ui'.
result.add(FontVariation.lerp(a[index], b[index], t));
^
../../../flutter/packages/flutter/lib/src/painting/text_painter.dart:345:51: Error: A value of type 'LineMetrics?' can't be assigned to a variable of type 'LineMetrics' because 'LineMetrics?' is nullable and 'LineMetrics' isn't.
- 'LineMetrics' is from 'dart:ui'.
final ui.LineMetrics lineMetrics = _paragraph.getLineMetricsAt(lastLineIndex);
^
../../../flutter/packages/flutter/lib/src/painting/text_painter.dart:1329:82: Error: The argument type 'TextDirection?' can't be assigned to the parameter type 'TextDirection' because 'TextDirection?' is nullable and 'TextDirection' isn't.
- 'TextDirection' is from 'dart:ui'.
final double paintOffsetAlignment = _computePaintOffsetFraction(textAlign, textDirection);
^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:726:1: Error: Type 'TextAffinity' not found.
TextAffinity? _toTextAffinity(String? affinity) {
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:796:18: Error: Type 'TextPosition' not found.
final (Offset, TextPosition)? startLocation;
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:1081:22: Error: Type 'TextPosition' not found.
void bringIntoView(TextPosition position);
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:728:34: Error: Undefined name 'TextAffinity'.
'TextAffinity.downstream' => TextAffinity.downstream,
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:729:34: Error: Undefined name 'TextAffinity'.
'TextAffinity.upstream' => TextAffinity.upstream,
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:796:18: Error: 'TextPosition' isn't a type.
final (Offset, TextPosition)? startLocation;
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:824:77: Error: Undefined name 'TextAffinity'.
affinity: _toTextAffinity(encoded['selectionAffinity'] as String?) ?? TextAffinity.downstream,
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/text_input.dart:1081:22: Error: 'TextPosition' isn't a type.
void bringIntoView(TextPosition position);
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/services/binding.dart:283:89: Error: The argument type 'AppLifecycleState?' can't be assigned to the parameter type 'AppLifecycleState' because 'AppLifecycleState?' is nullable and 'AppLifecycleState' isn't.
- 'AppLifecycleState' is from 'dart:ui'.
final List<AppLifecycleState> generated = _generateStateTransitions(lifecycleState, state);
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:595:9: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
_currentFocalPoint,
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:602:9: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
_currentFocalPoint,
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:647:48: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
pointerStartLocation: _pointerLocations[_pointerQueue[0]],
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:649:46: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
pointerEndLocation: _pointerLocations[_pointerQueue[1]],
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:655:48: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
pointerStartLocation: _pointerLocations[_pointerQueue[0]],
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:657:46: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
pointerEndLocation: _pointerLocations[_pointerQueue[1]],
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:664:26: Error: A value of type 'Offset?' can't be assigned to a variable of type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
_initialFocalPoint = _currentFocalPoint;
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:728:25: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
focalPoint: _currentFocalPoint,
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:745:23: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
focalPoint: _currentFocalPoint,
^
../../../flutter/packages/flutter/lib/src/gestures/scale.dart:761:30: Error: A value of type 'Offset?' can't be assigned to a variable of type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
_initialFocalPoint = _currentFocalPoint;
^
../../../flutter/packages/flutter/lib/src/gestures/monodrag.dart:436:50: Error: A value of type 'Offset?' can't be assigned to a variable of type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
final Offset offset = _moveDeltaBeforeFrame[pointer];
^
../../../flutter/packages/flutter/lib/src/gestures/monodrag.dart:454:48: Error: A value of type 'Offset?' can't be assigned to a variable of type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
final Offset offset = _moveDeltaBeforeFrame[pointer];
^
../../../flutter/packages/flutter/lib/src/gestures/multidrag.dart:147:14: Error: The argument type 'Offset?' can't be assigned to the parameter type 'Offset' because 'Offset?' is nullable and 'Offset' isn't.
- 'Offset' is from 'dart:ui'.
delta: pendingDelta,
^
../../../flutter/packages/flutter/lib/src/painting/placeholder_span.dart:58:9: Error: Type 'TextBaseline' not found.
final TextBaseline? baseline;
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/placeholder_span.dart:58:9: Error: 'TextBaseline' isn't a type.
final TextBaseline? baseline;
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/placeholder_span.dart:79:33: Error: 'TextBaseline' isn't a type.
properties.add(EnumProperty<TextBaseline>('baseline', baseline, defaultValue: null));
^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/colors.dart:487:85: Error: The argument type 'Color?' can't be assigned to the parameter type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
swatch = a!._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(color, null, t)));
^
../../../flutter/packages/flutter/lib/src/painting/colors.dart:490:86: Error: The argument type 'Color?' can't be assigned to the parameter type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
swatch = b._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(null, color, t)));
^
../../../flutter/packages/flutter/lib/src/painting/colors.dart:492:86: Error: The argument type 'Color?' can't be assigned to the parameter type 'Color' because 'Color?' is nullable and 'Color' isn't.
- 'Color' is from 'dart:ui'.
swatch = a._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(color, b[key], t)));
^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:377:3: Error: Type 'Path' not found.
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:377:21: Error: Type 'Rect' not found.
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:377:33: Error: Type 'TextDirection' not found.
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
^^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:390:3: Error: Type 'Path' not found.
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:390:21: Error: Type 'Rect' not found.
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:390:33: Error: Type 'TextDirection' not found.
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
^^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:402:14: Error: Type 'Canvas' not found.
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:402:29: Error: Type 'Rect' not found.
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:402:41: Error: Type 'TextDirection' not found.
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
^^^^^^^^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:454:3: Error: Type 'Offset' not found.
Offset valley;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:455:3: Error: Type 'Offset' not found.
Offset point;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:456:3: Error: Type 'Offset' not found.
Offset valleyArc1;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:457:3: Error: Type 'Offset' not found.
Offset pointArc1;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:458:3: Error: Type 'Offset' not found.
Offset pointArc2;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:459:3: Error: Type 'Offset' not found.
Offset valleyArc2;
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:488:3: Error: Type 'Path' not found.
Path generate(Rect rect) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:488:17: Error: Type 'Rect' not found.
Path generate(Rect rect) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:535:14: Error: Type 'Offset' not found.
required Offset center,
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:626:20: Error: Type 'Path' not found.
void _drawPoints(Path path, List<_PointInfo> points) {
^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:659:20: Error: Type 'Offset' not found.
double _getAngle(Offset a, Offset b, Offset c) {
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:659:30: Error: Type 'Offset' not found.
double _getAngle(Offset a, Offset b, Offset c) {
^^^^^^
../../../flutter/packages/flutter/lib/src/painting/star_border.dart:659:40: Error: Type 'Offset' not found.
double _getAngle(Offset a, Offset b, Offset c) {
^^^^^^
...
...
...
...
../../../flutter/packages/flutter/lib/src/material/about.dart:1108:37: Error: The argument type '_PageOpener?' can't be assigned to the parameter type '_PageOpener' because '_PageOpener?' is nullable and '_PageOpener' isn't.
- '_PageOpener' is from 'package:flutter/src/material/about.dart' ('../../../flutter/packages/flutter/lib/src/material/about.dart').
return _MasterDetailFlowProxy._(pageOpener);
^
Target kernel_snapshot failed: Exception
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\Programming\flutter\bin\flutter.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 5s
Error: Gradle task assembleDebug failed with exit code 1
Exited (1).
I solved my problem by completely removing flutter and dart and reinstalling it. I thought I had done that when reinstalling VSCode etc. but apparently didn't clean up enough.
So the old saying "close all windows and reboot" also holds this time ;-)
Thanks anyway, best regards, Sebastian