Home > Back-end >  I am getting this error "The instance member 'setState' cant be accessed in an initia
I am getting this error "The instance member 'setState' cant be accessed in an initia

Time:08-08

I am getting this error "The instance member 'setState' cant be accessed in an initializer" here : enter image description here

This is the full code :

class insertLossEvent extends StatefulWidget {
  @override
  State<insertLossEvent> createState() => _insertLossEventState();
}

class _insertLossEventState extends State<insertLossEvent> {
  static final field_1Controller = TextEditingController();
  static final field_4Controller = TextEditingController();

  void _sendLogin() async {
    var newMessage = await (ReadCache.getString(key: 'cache1'));

    var map = <String, dynamic>{
      "Field_1": field_1Controller.text,
      "Field_4": field_4Controller.text,
      "table_id": "25510",
      "form_type": "2",
    };

    var res = await http.post(
      Uri.parse("http://192.168.1.4:8080/HongLeong/SET_FORM_REQUEST.do"),
      headers: {HttpHeaders.cookieHeader: newMessage},
      body: map,
    );

    final data = jsonDecode(res.body);
    print(data);
  }

  int _selectedIndex = 0;

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


  static List<String> orgUnit = [
    'M1',
    'M100138',
    'M100078',
    'M100108',
    'M100069',
    'M100118',
    'M100071',
    'M100150'
  ];
  
  static String valueChoose;

  final List<Widget> _pages = [
    Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            //Main Info
            ListTileTheme(
              tileColor: Colors.grey,
              child: ExpansionTile(
                title: Text('Main Info',
                    style: GoogleFonts.poppins(
                        textStyle: const TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ))),
                children: [
                  //Fields
                  ListTileTheme(
                    tileColor: Colors.white,
                    child: ListTile(
                      title: Text(
                        'Title : ',
                        style: GoogleFonts.poppins(
                          textStyle: const TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 20,
                          ),
                        ),
                      ),
                    ),
                  ),
                  ListTileTheme(
                    tileColor: Colors.white,
                    child: ListTile(
                      title: Container(
                        decoration: BoxDecoration(
                          color: Colors.transparent,
                          border: Border.all(color: Colors.blueGrey),
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: TextField(
                            decoration: InputDecoration(
                                border: InputBorder.none,
                            ),
                            controller: field_1Controller,
                            style: GoogleFonts.poppins(
                              textStyle: const TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 20,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  ListTileTheme(
                    tileColor: Colors.white,
                    child: ListTile(
                      title: Text(
                        'Organization Unit : ',
                        style: GoogleFonts.poppins(
                          textStyle: const TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 20,
                          ),
                        ),
                      ),
                    ),
                  ),
                  ListTileTheme(
                    tileColor: Colors.white,
                    child: ListTile(
                      title: Container(
                        decoration: BoxDecoration(
                          color: Colors.transparent,
                          border: Border.all(color: Colors.blueGrey),
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: DropdownButton<String>(
                            value: valueChoose,
                            onChanged: (newValue){
                              setState(() {
                                valueChoose = newValue;
                              });
                            },
                            items: orgUnit.map((valueItem) {
                              return DropdownMenuItem(
                                value: valueItem,
                                child: Text(valueItem),
                              );
                            }).toList(),
                        ),
                      ),
                    ),
                  ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    ),
    Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            //Shariah Details
            ListTileTheme(
              tileColor: Colors.grey,
              child: ExpansionTile(
                title: Text('Shariah Details',
                    style: GoogleFonts.poppins(
                        textStyle: const TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ))),
                children: [
                  //Fields
                ],
              ),
            ),
          ],
        ),
      ),
    )
  ];

In the best case scenario, I'd like to retain the code as this, but if you know of any improvements, please let me know. How can I make it better? & what causes it to take place? Thank You

CodePudding user response:

Define pages like

 List<Widget> _pages = [];

Then in initstate add values to it

_pages = [// add values here
 ];
 setState((){});

The issue is that you tried adding set state when the codes are initialising. Adding it in initatate will not theow this error

CodePudding user response:

Using late keyword will fix this issue like

late final List<Widget> _pages = [

But I will suggest to use method instead of variables in this case. Using variable doesn't get update context until you reassign the variable.

Do like this is better

  List<Widget> _pages(BuildContext context) => [

CodePudding user response:

to solve the problem, just add the pages List in the build Function and add return Container();

@override
Widget build(BuildContext context) {
    final List<Widget> _pages = [
       ....
    ];
    return Container();
}
  • Related