0

I am creating app in Flutter in which I need to save data coming from textinput through shared preferences in JSON Format. I searched about JSON serialization in dart before saving it to the shared preferences I created a Model class for serializing JSON which is given below :

class ServerData {
  final String servername;
  final String serverurl;
  final String username;
  final String password;

  ServerData(this.servername, this.serverurl, this.username, this.password);

  ServerData.fromJson(Map<String, dynamic> json)
      : servername = json['servername'],
        serverurl = json['serverurl'],
        username = json['username'],
        password = json['password'];

  Map<String, dynamic> toJson() => {
    'servername' : servername,
    'serverurl' : serverurl,
    'username' : username,
    'password' : password
  };
}

Now I need to store the input data with the help of TextEditingController :

final _servername = TextEditingController();
final _serverurl = TextEditingController();
final _username = TextEditingController();
final _password = TextEditingController();

ServerData serverData = new ServerData(_servername.text,_serverurl.text,_username.text,_password.text); 

/* I am getting error of "Only static members can be accessed in Initializers" in above code */

String encodeData = jsonEncode(serverData); /* Not able to use encode and decode due to error in ServerData object */

I need help regarding this.

5
  • You can watch solution, but I don't understand. All of your controllers are just created and empty. So, you are creating ServerData with empty strings Commented Jul 31, 2019 at 11:15
  • Actually I was trying to store all the textinput in serverdata through textediting controller but I am doing it in wrong because I am initialising it with empty text inputs. So do I need to setState first for all the inputs? Commented Aug 1, 2019 at 8:59
  • You have to create ServerData in moment when you exactly want to save data. It can by on pressing the button or on changing text in TextField Commented Aug 1, 2019 at 9:17
  • Yes, I was able to store data now json format Thanks for that. But what if I want to store another Json data in the same key of shared preference and it should look like a list. Commented Aug 2, 2019 at 8:14
  • It's not the good practice - store different formats in one list. It would be better to save with different keys. But if you really want - you can put your data to jsonArray and save it, so it will be list Commented Aug 2, 2019 at 8:42

1 Answer 1

1

You have to move initialization inside initState:

ServerData serverData;
String encodeData;

@override
void initState() {
  serverData = new ServerData(_servername.text,_serverurl.text,_username.text,_password.text);
  encodeData = jsonEncode(serverData);
  super.initState();
}
Sign up to request clarification or add additional context in comments.

Comments

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.