0

我试图将 textEditing 控制器的值保存在 sharedPrefrences 中,我将它的值保存为字符串,但后来我不知道如何检索字符串的值并将其设置回 textEditingController ,这是我的代码:

  void saveSubmitScreenPrefs(String phone, String number, String neighborhood,
      String houseNumber, String streetNumber) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("phone", phone);
    prefs.setString("number", number);
    prefs.setString("neighborhood", neighborhood);
    prefs.setString("houseNumber", houseNumber);
    prefs.setString("streetNumber", streetNumber);

    print("saved the sumbit fields!");
  }

  void loadSubmitScreenPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    nameController = prefs.get("name").;
    phoneNumberController = prefs.get("phone");
    neighborhoodController = prefs.get("neighborhood");
    streetNumberController = prefs.get("streetNumber");
    houseNumberController = prefs.get("houseNumber");
    notifyListeners();
  }
4

2 回答 2

0

我用这个修复了它:

  void loadSubmitScreenPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    nameController = TextEditingController(text: prefs.get("name"));
    phoneNumberController = TextEditingController(text: prefs.get("phone"));
    neighborhoodController =
        TextEditingController(text: prefs.get("neighborhood"));
    streetNumberController =
        TextEditingController(text: prefs.get("streetNumber"));
    houseNumberController =
        TextEditingController(text: prefs.get("houseNumber"));
    notifyListeners();
  }
}
于 2020-07-01T21:21:53.323 回答
0

您可以通过使用控制器文本来实现这一点

   final TextEditingController textFieldController = TextEditingController();

    @override
    void initState() {
      super.initState();
      initialFormValues();
    }

    // set value in textfield  
    Future<void> initialFormValues() async {
        myNameController..text =  await Network.getValueFromStorage("name") ?? "";
    }
于 2020-07-01T21:15:02.340 回答