3

I'm working on a project using bloc pattern and I have a problem with the initial state of my home page state class. Here's the code:

class HomeState extends Equatable {
  const HomeState({
    this.screenTittle = LocaleKeys.mainMenu.tr(),
    this.screenIndex = 0,
    this.showBurguerMenu = false,
  });

  final String screenTittle;
  final int screenIndex;
  final bool showBurguerMenu;

  @override
  List<Object?> get props => [screenTittle, screenIndex, showBurguerMenu];

  HomeState copyWith({
    String? screenTittle,
    int? screenIndex,
    bool? showBurguerMenu,
  }) {
    return HomeState(
      screenTittle: screenTittle ?? this.screenTittle,
      screenIndex: screenIndex ?? this.screenIndex,
      showBurguerMenu: showBurguerMenu ?? this.showBurguerMenu,
    );
  }
}

this is the state class and the problem is with

this.screenTiitle = LocalKeys.mainMenu.tr()

because im using localKeys to translate a text and i'm getting this error

 The default value of an optional parameter must be constant.

because the .tr() makes the translation converts the localKeys in not constant and I need to set a text that can be translated in the initial state.

I tried removing the const of the constructor , also the final of the variable and defining the initial state in the variable, I also tried to make a method and called it from the constructor but I always got the same error https://i.stack.imgur.com/O94Ao.png

Is there a way to make the builder accept and non constant value?

Bilal Saeed
  • 2,092
  • 8
  • 31
F7085
  • 49
  • 3

1 Answers1

0

Can you try to add const before assigning a default value like this

this.screenTittle =  const LocaleKeys.mainMenu.tr(),
  • I tried it and it dind't work. The error changed (The name 'LocaleKeys.mainMenu' isn't a class. Try correcting the name to match an existing class.) and the class is already imported and it doesn't matter how many time i imported the class it doesn't read it – F7085 Sep 06 '22 at 16:07