【问题标题】:Flutter Dropdown Button not saving state during Screen NavigationFlutter Dropdown Button在屏幕导航期间不保存状态
【发布时间】:2021-12-21 20:52:42
【问题描述】:

您好,我遇到了一个问题,我无法保存 DropdownButton 的状态(下拉按钮本身中显示的文本,这是我选择的值)。我正在使用 GetX 进行状态导航。

由于我的代码的结构方式,当我从一个屏幕导航到第二个屏幕并希望返回时,我使用Get.to(First() 而不是Get.back()

这是我的逻辑:

  1. 我会将用户从 DropdownButton 中选择的值保存到 SharedPreferences 中。由于按钮的性质,该值也将立即在按钮本身上显示给用户。
  2. 当用户离开(例如他现在在第二个屏幕上)并再次访问第一个屏幕时,我将从 SharedPreferences 加载选定的值并构造 DropdownButton 项,以便该值将显示在顶部。李>

我面临的问题: 从 SharedPreferences 检索数据是异步的,所以在等待从 SharedPreferences 检索数据时,我不知道如何再次构造 FirstScreen。谁能帮帮我?谢谢!

【问题讨论】:

    标签: flutter dart asynchronous constructor dropdownbutton


    【解决方案1】:

    使用 GetStorage() 代替 shared-preference 。没有异步问题

    dependencies:
      get_storage: ^2.0.3
    
    
    use GetStorage through an instance or use directly GetStorage().read('key')
    final box = GetStorage();
    
    To write information you must use write :
    
    box.write('quote', 'GetX is the best');
    
    
    To read values you use read:
    
    
    print(box.read('quote'));
    // out: GetX is the best
    

    【讨论】:

    • 成功了,谢谢!
    • 请点赞。欢迎!
    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2019-06-07
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多