Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The instance member 'widget' can't be accessed in an initializer – flutter

I am trying to pass data to another screen but I am faced with this error.

The instance member ‘widget’ can’t be accessed in an initializer.
Try replacing the reference to the instance member with a different expression

A number of users have posted a similar question, and I have actually gone through most of them but none of those given solutions seems to work in my case

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried without the widget but it still didn’t work. I mean I tried this department.name

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key, required this.department}) : super(key: key);
  final Department department;

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  
  int _selectedIndex = 0;
  static List<Widget> _widgetOptions = <Widget>[
    HomeView(
      department: Department(
          name: widget.department.name,// The error is here. 
          stream: '',
          description: '',
          author: '',
          availability: true,
          hod: '',
          totalNoBooks: 0),
    ),
    RequestedBooks(),
    ProfileScreen(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Center(
          child: Text("Some Text Here"),
        ),
      ),
    );
  }
}

I don’t have a problem with the receiving screen. This is an excerpt of the code in the receiving screen

class HomeView extends StatefulWidget {
  const HomeView({
    Key? key,
    required this.department,
  }) : super(key: key);

  final Department department;

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

>Solution :

Try adding widgets in the initstate

late List<Widget> _widgetOptions = [];
@override 
void initState(){
_widgetOptions = <Widget>[
    HomeView(
      department: Department(
          name: widget.department.name,// The error is here. 
          stream: '',
          description: '',
          author: '',
          availability: true,
          hod: '',
          totalNoBooks: 0),
    ),
    RequestedBooks(),
    ProfileScreen(),
  ];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading