0

I am trying to pass the parameter username which has been received from another class to my HomePage class. I want to pass that parameter from the HomePageState to another class called Profile.

I tried using the "widget.username" method to get access to the username parameter from the _HomePageState. But it gives an error saying "The instance member 'widget' can't be accessed in an initializer".

Below is the code of the HomePage class for your reference:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:codeforces_data_flutter/screens/profile.dart';

class HomePage extends StatefulWidget {
  String username;
  HomePage({this.username});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const widgetStyle =
      TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0);

  int selectedIndex = 0;
  List<Widget> widgetsToDisplay = [
    Profile(
      username: widget.username,             /*Error is on this line*/
    ),
    Center(
      child: Text(
        'Index 1 : Previous Contests',
        style: widgetStyle,
      ),
    ),
    Center(
      child: Text(
        'Index 2 : Upcoming Contests',
        style: widgetStyle,
      ),
    ),
    Center(
      child: Text(
        'Index 3 : About Me',
        style: widgetStyle,
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.profile_circled,
            ),
            label: 'User Profile',
            backgroundColor: Colors.teal,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.arrow_counterclockwise,
            ),
            label: 'Previous Contests',
            backgroundColor: Colors.blue,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.arrow_clockwise,
            ),
            label: 'Upcoming Contests',
            backgroundColor: Colors.green,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.suit_heart,
            ),
            label: 'About Me',
            backgroundColor: Colors.pink,
          ),
        ],
        currentIndex: selectedIndex,
        selectedItemColor: Colors.yellow,
        onTap: (index) {
          setState(() {
            selectedIndex = index;
          });
        },
      ),
      body: widgetsToDisplay[selectedIndex],
    );
  }
}

The problem lies in the widgetsToDisplay list when the Profile class is called with passing widget.username for the username property.

I have marked the line which gives the error using a comment.

Any help is appreciated. Thank You.

Dipanshu
  • 15
  • 4

2 Answers2

1

i had the same problem a while ago. Try this:

class HomePage extends StatefulWidget {
final username;
HomePage({this.username});

  @override
  _ HomePage State createState() => _ HomePage State(this.username);
}

class _HomePageState extends State<HomePage> {
  _HomePageState(String username) {
    this._username = username;
  }
  String _username;

Now you can use _username inside your build method.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Raphael
  • 33
  • 1
  • 6
  • Thank you Raphael for answering. Actually, I was calling the Profile class outside the Build method and was facing the problem because of that. It helped. When I shifted the list inside the build method, the error was gone. – Dipanshu May 02 '21 at 10:58
0

Your compiler is correct, you cannot access that variable in an initializer, so do not make it an initializer when you don't have to.

Move your widget building code (in this instance the list) into the build function, or into a separate function that is then called in the build function.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • It worked perfectly, thank you. But can you please explain a bit that why it was not working previously and how adding the list inside the build method worked? – Dipanshu May 02 '21 at 10:50