0

Looking to get some clarity as this code is driving me crazy. I am passing data between screens in flutter using an object. Here is what my model file looks like

models.dart

 class CompInfo<Map> {
  String uid;
  String email;
  String companyName;
  String companyAddress;
  bool needLandscapingHelp;
  bool needLawnServicesHelp;

CompInfo(
      {this.uid = '',
      this.email = '',
      this.companyName = '',
      this.companyAddress = '',
      this.needLandscapingHelp=false,
      this.needLawnServicesHelp=false,};
}

Screen one: user enters company name and address

 Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => CompanyProject(
                          companyinfo: sendCompanyInfo(
                              companyName.text, comapanyAddress.text))));

helper function on screen 1

CompInfo(compName, compAddress) {
  CompInfo compinfo =
      CompInfo(companyName: compName, companyPitch: compPitch);
  return compinfo;
}

Screen 2:

class CompanyProject extends StatefulWidget {
  CompanyInfo companyinfo;
  CompanyProject({Key? key, required this.companyinfo}) : super(key: key);
  @override
  State<CompanyProject> createState() => _CompanyProjectState();
}

class _CompanyProjectState extends State<CompanyProject> {
  @override
  void initState() {
    compinfo=widget.companyinfo;
    super.initState();
  }

  late CompanyProject compinfo;

  TextEditingController tellusmore = TextEditingController();

  bool mvpBoxyes = false;
  bool mvpBoxno = false;
  bool errorprompt = false;
  List checkboxlist = [
    CheckBoxState(
      title: 'Lawn',
      icon: Icons.computer,
    ),
    CheckBoxState(
        title: 'Garden', icon: Icons.smartphone, widgettitle: compinfo.needLandscapingHelp),
   ]

etc...

I'm basically creating checkboxes and would like the model to update with the value. However the widget title: compinfo.needLandscapingHelp keeps throwing "The instance member 'compinfo' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression"

Any ideas?

SKS81
  • 117
  • 2
  • 13

1 Answers1

1

You have to assign compinfo value either inside initState or inside a function, the reason for this is that instance members of the class cannot be re-assign inside the class initializer, so you have to assign it after the class has been initialize which is after the initState lifecycle:

class _CompanyProjectState extends State<CompanyProject> {
  @override
  void initState() {
    compinfo=widget.companyinfo;

   checkboxlist = [
    CheckBoxState(
      title: 'Lawn',
      icon: Icons.computer,
    ),
    CheckBoxState(
        title: 'Garden', icon: Icons.smartphone, widgettitle: compinfo.needLandscapingHelp),
   ]

    super.initState();

  }

  late CompanyProject compinfo;

  TextEditingController tellusmore = TextEditingController();

  bool mvpBoxyes = false;
  List checkboxlist;
  bool mvpBoxno = false;
  bool errorprompt = false;
Wilson Toribio
  • 990
  • 1
  • 4
  • 15
  • To clarify the above, you can only use a variable to set the value of another variable in a function. Any function will do. --- final test = 'test; --- final test2 = test; //Bad --- void setTest2(){ test2 = test;} //Good – scottstoll2017 Apr 07 '22 at 14:43
  • Correct, you can find more information here: https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=implicit_this_reference_in_initializer#implicit_this_reference_in_initializer – Wilson Toribio Apr 07 '22 at 14:47
  • Another thing is that at that point of the program variables are being initialize so you cannot assign their value to others variables because they do not have values yet, that is the reason for putting the assignment inside initState, which happens after all variables are ready and done. – Wilson Toribio Apr 07 '22 at 14:50
  • thanks everyone. The problem now is I cant access the checkbox list in my build state. I'm basically using a list builder to render the name, the icon and checkbox value. I declared the variable as well but not working. Ideas? – SKS81 Apr 07 '22 at 16:51
  • @WilsonToribio yeah thanks I tried that but cant access the variable Checkboxlist in the build method. How would you do this with a function? – SKS81 Apr 07 '22 at 23:40
  • Would you show the code, on how are you trying to do it? – Wilson Toribio Apr 08 '22 at 11:53