Flutter エラー「Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)」が発生した場合の対処法

Flutter エラー「Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)」が発生した場合の対処法

Flutterで、エラー「Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)」が発生した場合の対処法を記述してます。「Widget」の「Text」などに変数が使用されている際に発生します。

環境

  • OS  windows11 pro 64bit
  • Flutter 3.3.7

エラー全文

「Text」に変数を使用しようとした際に発生

const Text(foo),

エラー全文

dynamic responseResult
Type: dynamic package:flutter_application/main.dart

Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)
Arguments of a constant creation must be constant expressions.
Try making the argument a valid constant, or use 'new' to call the constructor.dartconst_with_non_constant_argument
A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor.
Try using a subtype, or removing the keyword 'const'.dartconst_constructor_param_type_mismatch

画像

原因

「const」が設定されているため。親のWidgetに「const」が設定されていても発生します。

対処法

「const」を外します。

Text(foo),