4

My tests are failing to compile and run in a Dart only project that is referenced by my Flutter project. I'm receiving the following error message

Failed to precompile test:test:
../../../../../../../../../.pub-cache/hosted/pub.dartlang.org/analyzer-1.0.0/lib/src/error/best_practices_verifier.dart:1952:14: Error: A non-null value must be returned since the return type 'String' doesn't allow null.
  String get displayString {

Any ideas please?

Flutter 2.2.1 (current stable channel)
Tools • Dart 2.13.1

(I've asked the question in Flutter's github here also) https://github.com/flutter/flutter/issues/83683

atreeon
  • 21,799
  • 13
  • 85
  • 104
  • 2.13.1 (Flutter stable). I think the displayString function used by Dart during compile (its not a function I wrote) – atreeon Jun 07 '21 at 09:44
  • just to confirm the offending function is in the analyzer package in the following file ```pub.dartlang.org/analyzer-1.0.0/lib/src/error/best_practices_verifier.dart``` – atreeon Jun 07 '21 at 09:52

3 Answers3

5

There was a component using analyzer version 1.0.0

Upgrading this component to use analyzer version 1.7 or above seemed to fix the problem for me.

atreeon
  • 21,799
  • 13
  • 85
  • 104
  • In case you don't see the analyzer in your pubspec.yaml file, add it: to dev_dependencies: analyzer: ^1.7.0 – stoiczek Jul 05 '21 at 01:58
2

go to this file: flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.5.0/lib/src/error/best_practices_verifier.dart:1978:14.

and add below:-

default:
    return '';
  • Ugly but works. Fun thing is that Master channel of 2.1.0 (as of date of this comment) handles this situation ok and builds successfully – jujka Jun 24 '21 at 06:45
  • The solution to upgrade version of analyzer is better than hacking around 3rd party sources (that can be overrun at any time and work only in local workspace). – stoiczek Jul 05 '21 at 01:59
0

Please check the function displayString to make sure that it returns non-null string variable. There are some use cases that I guess you might face with

  1. Use "required" if you get the variable from parameters (ex: String displayString(required String var)).
  2. If the variable is optional parameter, then you need to check if it is null or not. Then, you could "return var!;" to let the function knows that you already confirmed the variable content.
  3. If you want to return nullable String, then you should change the function to "String? displayString".
Phan Hai Quang
  • 709
  • 6
  • 7