Fix library-scope expression evaluation. (#18851)

This also improves diagnostic in case of internal compiler errors.
diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart
index 9eeb8df..0c07e55 100644
--- a/packages/flutter_tools/lib/src/run_hot.dart
+++ b/packages/flutter_tools/lib/src/run_hot.dart
@@ -116,12 +116,12 @@
         final CompilerOutput compilerOutput =
             await device.generator.compileExpression(expression, definitions,
                 typeDefinitions, libraryUri, klass, isStatic);
-        if (compilerOutput.outputFilename != null) {
+        if (compilerOutput != null && compilerOutput.outputFilename != null) {
           return base64.encode(fs.file(compilerOutput.outputFilename).readAsBytesSync());
         }
       }
     }
-    return null;
+    throw 'Failed to compile $expression';
   }
 
   Future<int> attach({
diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart
index c33beb0..9547e34 100644
--- a/packages/flutter_tools/lib/src/vmservice.dart
+++ b/packages/flutter_tools/lib/src/vmservice.dart
@@ -163,7 +163,7 @@
         final List<String> definitions = params['definitions'].asList;
         final List<String> typeDefinitions = params['typeDefinitions'].asList;
         final String libraryUri = params['libraryUri'].asString;
-        final String klass = params['klass'] != null ? params['klass'].asString : null;
+        final String klass = params['klass'].exists ? params['klass'].asString : null;
         final bool isStatic = params['isStatic'].asBoolOr(false);
 
         try {