Flutter: Passing AppLocalizations.of(context)!.actionaid into initalUrl of a webview

Issue

I am currently localizing my app, I have translates all the webpages to be opened and added the url to my arb files.

But when i call the AppLocalizations.of(context)!.actionaid on initialUrl i get a red underline saying Invalid constant value

Is there any way I could make this work?

Thanks guys

I have something like this:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class actionAid extends StatelessWidget {
 const actionAid({Key? key}) : super(key: key);

  @override
Widget build(BuildContext context) {
return const CupertinoPageScaffold(
  navigationBar: CupertinoNavigationBar(middle: Text("Action Aid")),
  child: SafeArea(
    child: WebView(
      initialUrl:  AppLocalizations.of(context)!.actionaid,
      javascriptMode: JavascriptMode.unrestricted,
    ),
    //bottomNavigationBar: CustomBottomNavBar(selectedMenu: MenuState.give),
  ),
);
}}

Solution

The problem is that you are defining your Widget tree to only contain constant values by writing const CupertinoPageScaffold.

Since AppLocalization.of(context).actionaid is not providing a constant value this "rule" gets violated and triggers the error.

Changing const CupertinoPageScaffold to just CupertinoPageScaffold or extracting the WebView widget into a separate widget with a const constructor should do the trick.

Answered By – daniellampl

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *