Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Not a constant expression(Flutter)

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
like image 653
boybeak Avatar asked Jan 24 '26 07:01

boybeak


1 Answers

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;
}
like image 128
Darja Orlova Avatar answered Jan 26 '26 00:01

Darja Orlova



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!