Skip to content

error: Not a constant expression - only at runtime #33883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zoechi opened this issue Jul 17, 2018 · 7 comments
Open

error: Not a constant expression - only at runtime #33883

zoechi opened this issue Jul 17, 2018 · 7 comments
Labels
legacy-area-front-end Legacy: Use area-dart-model instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@zoechi
Copy link
Contributor

zoechi commented Jul 17, 2018

I use a class with a const constructor with new and it complains that it is not a constant expression even though none should be required.

I'm also pretty sure I didn't make changes related to this code since a few months and it worked so far.

DartAnalyzer doesn't show any hints or warnings

here _createSplashScreen() is called

class AppWidgetState extends State<AppWidget> {
  @override
  Widget build(BuildContext context) => new StreamBuilder<AppController>(
      stream: widget.appController.onStateChange,
      builder: (context, _) => widget.appController.isSplashScreenVisible
          ? _createSplashScreen()
          : _createApp(context));

the implementation

  MaterialApp _createSplashScreen() =>
      new MaterialApp(home: new SplashScreen.tenant());

the class with the const constructor

class SplashScreen extends StatelessWidget {
  const SplashScreen.base();

  factory SplashScreen.tenant() {
    switch (Tenant.tenant) {
      case Tenant.origin:
        return const SplashScreenOrigin();
      default:
        return const SplashScreen.base();
    }
  }
class SplashScreenOrigin extends SplashScreen {
  const SplashScreenOrigin() : super.base();

The error message is not very helpful. See also #33438

I/flutter (13702): 8 2018-07-17 14:29:27.436094 SEVERE lookatmybaby_flutter.sentry_error_reporter: Context: building StreamBuilder<AppController<EntityMixin<dynamic>>>(dirty, state: _StreamBuilderBaseState<AppController<EntityMixin<dynamic>>, AsyncSnapshot<AppController<EntityMixin<dynamic>>>>#05069), library: widgets library
I/flutter (13702):     error: 'package:lookatmybaby_flutter/widgets/screens/splash_screen.dart': error: Not a constant expression.
I/flutter (13702): #0      AppWidgetState._createSplashScreen (package:lookatmybaby_flutter/widgets/app_widget.dart:134:33)
I/flutter (13702): #1      AppWidgetState.build.<anonymous closure> (package:lookatmybaby_flutter/widgets/app_widget.dart:47:13)
I/flutter (13702): #2      StreamBuilder.build (package:flutter/src/widgets/async.dart:408:74)
I/flutter (13702): #3      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
I/flutter (13702): #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:3743:27)
I/flutter (13702): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3655:15)
I/flutter (13702): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3508:5)
I/flutter (13702): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3635:5)
I/flutter (13702): #8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3782:11)
I/flutter (13702): #9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
I/flutter (13702): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
@zoechi
Copy link
Contributor Author

zoechi commented Jul 17, 2018

[✓] Flutter (Channel master, v0.5.8-pre.58, on Mac OS X 10.13.6 17G65, locale en-AT)
• Flutter version 0.5.8-pre.58 at /Users/zoechi/flutter/flutter
• Framework revision 85f09f6 (9 hours ago), 2018-07-16 22:42:06 -0700
• Engine revision c5a63d28bf
• Dart version 2.0.0-dev.67.0.flutter-84ca27a09e

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
• Android SDK at /usr/local/opt/android-sdk
• Android NDK at /usr/local/opt/android-sdk/ndk-bundle
• Platform android-27, build-tools 27.0.3
• ANDROID_HOME = /usr/local/opt/android-sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
• All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.4.1, Build version 9F2000
• ios-deploy 1.9.2
• CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 23.1.2
• Dart plugin version 173.4700
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] IntelliJ IDEA Ultimate Edition (version 2018.1.5)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 26.0.2
• Dart plugin version 181.4892.1

[✓] Connected devices (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 8.1.0 (API 27) (emulator)

• No issues found!

@lrhn lrhn added the legacy-area-front-end Legacy: Use area-dart-model instead. label Jul 18, 2018
@zoechi zoechi changed the title error: Not a constant expression. error: Not a constant expression - only at runtime Jul 19, 2018
@zoechi
Copy link
Contributor Author

zoechi commented Aug 22, 2018

Can't reproduce anymore.

@zoechi zoechi closed this as completed Aug 22, 2018
@zoechi zoechi reopened this Aug 23, 2018
@zoechi
Copy link
Contributor Author

zoechi commented Aug 23, 2018

Still the same. Made a mistake when testing.

@lrhn lrhn added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Aug 23, 2018
@kmillikin
Copy link

I can't reproduce this from just the snippets of code above.

Unfortunately, the same error message "Not a constant expression." is used in both the front end compiler and the Dart VM, so I can't even localize the problem from the (bad) stack trace.

Is it possible we could get a standalone reproduction?

@zoechi
Copy link
Contributor Author

zoechi commented Aug 23, 2018

Thanks for having a look.
I'll try to come up with a reproduction, but it might take a while.

@zoechi
Copy link
Contributor Author

zoechi commented Sep 3, 2018

The simplest reproduction I was able to come up with is

import 'package:flutter/widgets.dart';

void main() {
  print(SplashScreen.tenant());
}

abstract class SplashScreen extends StatelessWidget {
  const SplashScreen.base();

  factory SplashScreen.tenant() => const SplashScreenFoo();
}

class SplashScreenFoo extends SplashScreen {
  const SplashScreenFoo() : super.base();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
  }
}

I wasn't able to reproduce without extending from StatelessWidget, so this code can only run in Flutter.
I tried to replace StatelessWidget with a custom class built after StatelessWidget but I haven't found what part actually caused the issue.

@zoechi
Copy link
Contributor Author

zoechi commented Sep 3, 2018

Currently at

$ flutter doctor -v
[✓] Flutter (Channel master, v0.7.4-pre.21, on Mac OS X 10.13.6 17G65, locale en-AT)
• Flutter version 0.7.4-pre.21 at /Users/zoechi/flutter/flutter
• Framework revision 7cebaac (4 days ago), 2018-08-29 21:53:39 -0700
• Engine revision 252da48082
• Dart version 2.1.0-dev.3.0.flutter-760a9690c2

[✓] Android toolchain - develop for Android devices (Android SDK 28.0.1)
• Android SDK at /usr/local/opt/android-sdk
• Android NDK at /usr/local/opt/android-sdk/ndk-bundle
• Platform android-28, build-tools 28.0.1
• ANDROID_HOME = /usr/local/opt/android-sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
• All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.4.1, Build version 9F2000
• ios-deploy 1.9.2
• CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 23.1.2
• Dart plugin version 173.4700
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] IntelliJ IDEA Ultimate Edition (version 2018.2)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 26.0.3
• Dart plugin version 182.3569.4

[✓] Connected devices (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 8.1.0 (API 27) (emulator)

• No issues found!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-front-end Legacy: Use area-dart-model instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants