Home > Enterprise >  How to create multiple same late variables with listeners with different names in 1 block of code
How to create multiple same late variables with listeners with different names in 1 block of code

Time:10-27

I have these multiple late variables here that just means the same.

  late final TextEditingController rentStatus = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
  late final TextEditingController rentStartInput = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
  late final TextEditingController rentEndInput = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
  late final TextEditingController renterName = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
  late final TextEditingController renterContact = TextEditingController()
    ..addListener(() {
      setState(() {});
    });

I want to make it so that I can make them all at the same time in just 1 code block instead of these multiple blocks.

I tried doing this

late final TextEditingController rentStatus, renterName, rentStartInput, rentEndInput, renterContact = TextEditingController()
    ..addListener(() {
      setState(() {});
    });

But it resulted in Late Initialization Error.

CodePudding user response:

you have to initialize each value before coma example

 late final TextEditingController rentStatus = TextEditingController()
     ..addListener(() {
       setState(() {});
     }), renterName = TextEditingController()
     ..addListener(() {
       setState(() {});
     }), rentStartInput = TextEditingController()
     ..addListener(() {
       setState(() {});
     }), rentEndInput = TextEditingController()
     ..addListener(() {
       setState(() {});
     }), renterContact = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
  • Related