Home > Software engineering >  Error: Not a constant expression(Flutter)
Error: Not a constant expression(Flutter)

Time:10-16

body: const Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: WebView(
          initialUrl: "https://xxxx",
          navigationDelegate: (navigation) {
            return NavigationDecision.navigate;
          },
        ),
      ),

When compile, an error happened.

lib/main.dart:84:31: Error: Not a constant expression.
          navigationDelegate: (navigation) {
                              ^^^^^^^^^^^^

When I use flutter-2.2.0 and webview_flutter-2.0.13, the code above works fine. But after i upgraded flutter to 2.5.2 and webview_flutter to 2.0.13, the error happened. environment(flutter 2.5.2):

environment:
  sdk: ">=2.12.0 <3.0.0"
dependencies:
  flutter:
    sdk: flutter
  webview_flutter: 2.1.1

CodePudding user response:

You are trying to use a const constructor of the Center widget with not constant parameters.

By creating a widget with a const constructor, you specify that all of its fields will be defined at compile-time.

So, in your case, you need to remove const before the Center widget, because this is not constant:

navigationDelegate: (navigation) {
    return NavigationDecision.navigate;
}

CodePudding user response:

I have faced similar problems with some other packages when I moved to the latest Flutter versions as my package versions were not up to date with the Flutter version.

Run this command,

flutter pub upgrade --major-versions

It will upgrade your packages (in this case webview_flutter) to the latest version which are compatible with your current Flutter version.

This worked for me.

  • Related