From 50855a3dcbaa46a64ce4bec60d301f468dfe9ab1 Mon Sep 17 00:00:00 2001 From: Markus Richter <8398165+mqus@users.noreply.github.com> Date: Fri, 21 Aug 2020 23:53:21 +0200 Subject: [PATCH] Fix weird early return value and use early return on other function --- .../misc/extension/dart_object_extension.dart | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/floor_generator/lib/misc/extension/dart_object_extension.dart b/floor_generator/lib/misc/extension/dart_object_extension.dart index 8bb7e6e6..cc4bc94b 100644 --- a/floor_generator/lib/misc/extension/dart_object_extension.dart +++ b/floor_generator/lib/misc/extension/dart_object_extension.dart @@ -5,22 +5,31 @@ import 'package:meta/meta.dart'; extension DartObjectExtension on DartObject { /// get the String representation of the enum value, or the result of - /// [orElse] if the enum was not valid. [orElse] + /// [orElse] if the enum was not valid. String toEnumValueString({@required String orElse()}) { final interfaceType = type as InterfaceType; final enumValue = interfaceType.element.fields .where((element) => element.isEnumConstant) .map((fieldElement) => fieldElement.name) .singleWhere((valueName) => getField(valueName) != null, - orElse: orElse); - - return '$interfaceType.$enumValue'; + orElse: () => null); + if (enumValue == null) { + return orElse(); + } else { + return '$interfaceType.$enumValue'; + } } + /// get the ForeignKeyAction this enum represents, or the result of + /// [orElse] if the enum did not contain a valid value. ForeignKeyAction toForeignKeyAction({@required ForeignKeyAction orElse()}) { final enumValueString = toEnumValueString(orElse: () => null); - return ForeignKeyAction.values.singleWhere( - (foreignKeyAction) => foreignKeyAction.toString() == enumValueString, - orElse: orElse); + if (enumValueString == null) { + return orElse(); + } else { + return ForeignKeyAction.values.singleWhere( + (foreignKeyAction) => foreignKeyAction.toString() == enumValueString, + orElse: orElse); + } } }