Search code examples
flutterdartsharedpreferencesflutter-getxtexteditingcontroller

I can't retrieve the data from sharedprefrence string list


I am trying to find a way to retrieve data from sharedPrefrence StringList..

I have these TextEditingControllers in my Controller:

  late TextEditingController displayname;
  late TextEditingController jobtitle;
  late TextEditingController about;
  late TextEditingController email;
  late TextEditingController address;

  late List<String> info;
  MyServices myServices = Get.find();

before user move to the next page, i saved the content of each on controller on sharedPreference StringList:

   myServices.sharedPreferences.setStringList("profileinfo", [
        displayname.text,
        jobtitle.text,
        about.text,
        email.text,
        address.text, 
      ]);

Here the error occur, when i try initialy to retrive the saved data (if user back again to this page)

void onInit() {
    imagePath = "";
    if (myServices.sharedPreferences.getStringList("profileinfo") == null) {
      info = [];
      displayname = TextEditingController();
      jobtitle = TextEditingController();
      about = TextEditingController();
      email = TextEditingController();
      address = TextEditingController();

    } else {
      info = (myServices.sharedPreferences.getStringList("profileinfo"))!;
      displayname = TextEditingController(info[0]); // Here is the error
      jobtitle = TextEditingController();
      about = TextEditingController();
      email = TextEditingController();
      address = TextEditingController();
   
    }

    super.onInit();
  }

The error says "Too many positional arguments: 0 expected, but 1 found. Try removing the extra positional arguments, or specifying the name for named arguments."

I tried a lot of methods including using (.?) or (!) .. but none of them works.


Solution

  • You are filling TextEditingController in a wrong way, you forgot to specify text, try this:

    displayname = TextEditingController(text: info[0]);