Search code examples
flutterformsdarttexttextfield

Using existing answer as a hint text


i'm trying to display the existing answers that are stored in the list List<Data> data = []; as a hint text if they're available. currently i'm using this code: https://pastecode.io/s/bj9jkx06

the error is that if i answer the question number two, the hint text will show in the textfield number one.

enter image description here


Solution

  • I think the Data object is not required. I would simply use another map<String, String> where the key is the question, exactly as your others maps.

    Map<String, String> data = {}
    

    This requires the data loading inside the read method to be changed as follows:

    raw.forEach((key, value) {
       data[key] = value;
    });
    

    Now that your data are loaded, you can retrive the hint for the question in this way, using the question as the key:

    String hint = data[question] != null? data[question] : 'write something';
    

    If data doesn't contains record related to a specific question, it will return 'write something' string