1
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
boybeak
  • 417
  • 5
  • 19
  • Hi, Did you try upgrading the package as you have upgraded flutter version? – Ratnadeep Oct 15 '21 at 00:39
  • Only the `webview_flutter` is installed by myself, 2.1.1 is the latest version until now. Actually the project is created under flutter 2.5.2 environment. – boybeak Oct 15 '21 at 07:10

2 Answers2

6

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;
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Darja Orlova
  • 141
  • 4
0

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.

Ratnadeep
  • 1,125
  • 1
  • 15
  • 30