0

Here in my code mapResponse is a variable which can be null, so I want to check and if the mapResponse is null I want the page to navigate to different page in bottom navigation, below given is the code, but it throws the error saying "The instance member 'mapResponse' can't be accessed in an initializer."

Map? mapResponse

     final screens = [
        const LandingPage(),
        const ComingSoon(),
        const ComingSoon(),
        mapResponse==null?const ProfileDashBoard():const Home()
      ];
eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
Mr.Robot
  • 91
  • 12

3 Answers3

0

check this out:

Map? mapResponse

List screens = [];
initState((){
        screens =[
        const LandingPage(),
        const ComingSoon(),
        const ComingSoon(),
        mapResponse==null?const ProfileDashBoard():const Home()
      ];

});
Saeed Ghasemi
  • 141
  • 1
  • 10
0

You nee do it in initState like this:

  Map? mapResponse;
  List screens = [];

  @override
  void initState() {
    super.initState();
    screens = [
      const LandingPage(),
      const ComingSoon(),
      const ComingSoon(),
      mapResponse == null ? const ProfileDashBoard() : const Home(),
    ];
  }
eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
0

You can use late

late final screens = [

More about late keyword with declaration

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56