0

I have a variable which was declared initially and which will be changed after a drop down selection. I couldnot access the value after it has been changed by using for loop and c

   String _selectedRegNoUpdate = 'Reg No';
   String _selectedRegNoUpdate1 = 'Reg No';
   String _selectedRegNoUpdate2 = 'Reg No';
   String _selectedRegNoUpdate3 = 'Reg No';
   String _selectedRegNoUpdate4 = 'Reg No';
   String _selectedRegNoUpdate5 = 'Reg No';

 for(int i = item!['workedWith'].length; i < 6 ; i++){
                                                                                                      
   print(i.toString());                                                                                                        
   String workedWithPosition = '_selectedRegNoUpdate$i';
                                                                                                      
   print(workedWithPosition);  // this prints _selectedRegNoUpdate3
                                                                                                      
   print(_selectedRegNoUpdate4);   // this prints the value which is stored in _selectedRegNoUpdate4 which is 12345
                                                                                                      
   print(workedWithPosition);  //but this print Reg No3 not the value selected by the drop down selector
                                                                                                      
   print((_selectedRegNoUpdate+i.toString()).toString());
                                                                                                    
 };
3
  • Can you include the full code Commented Aug 28, 2022 at 16:03
  • The full code is long and would be hard to copy here. Which part of the code do you want me to share, the hole is like 1260 lines Commented Aug 28, 2022 at 16:17
  • Variable names ending in digits is a code smell. Those values should have been in a data structure of some kind. Commented Aug 28, 2022 at 17:20

1 Answer 1

1

You cannot, in Dart, point to a variable directly based on a String value (unless we use reflection with dart:mirrors which are not supported on lot of platforms).

What you should do instead is create a List<String> which contains your values. This list can you then search using an index number.

List<String> _selectedRegNoUpdate = [
  'Reg No 1',
  'Reg No 2',
  'Reg No 3',
  'Reg No 4',
  'Reg No 5',
  'Reg No 6',
];

void main() {
  for (int i = 0; i < _selectedRegNoUpdate.length; i++) {
    print(_selectedRegNoUpdate[i]);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the above answer. I added the drop down values as they were selected. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.