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

I am getting this error "The instance member 'widget' can't be accessed in an initializer." Flutter/Dart

This is my code :

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class viewLossEvent extends StatefulWidget {
  String code,
      
  viewLossEvent(
      {this.code});

  @override
  State<viewLossEvent> createState() => _viewLossEventState();
}

class _viewLossEventState extends State<viewLossEvent> {

  int _selectedIndex = 0;

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

  final List<Widget> _pages = [
    Container(
      child: Text(
          '${widget.code}'
      ),
    ),
  ];

the error that I am getting :

"The instance member 'widget' can't be accessed in an initializer."

I am getting the error right here :

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

enter image description here

can anyone help solve this and kindly explain why this is happening.

Thank You!

>Solution :

You cannot initialize a property by using widget.code. You should use initState instead :

List<Widget> _pages = [];

void initState() {
  super.initState();
  _pages = [
    Container(
      child: Text(
          '${widget.code}'
      ),
    ),
  ];
}
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