Dart 如何使用具有共享首选项的作用域_模型?

Dart 如何使用具有共享首选项的作用域_模型?,dart,flutter,Dart,Flutter,我正在使用scoped_模型将状态管理添加到我的聊天应用程序中 我的问题是如何使用具有共享首选项的作用域_模型。因此,应用程序启动模型状态是由共享首选项的值填充的。例如,存储的用户名将从共享首选项中检索,然后存储在UserModel用户名状态中 我已经看过但没有找到教程怎么办 我从电影中找到了这个样本: 这是最好的方法吗? 最好的方法是什么 谢谢 我知道这个问题已经问了很久了。但我还是会回答的 当我使用共享首选项和范围模型开发android应用程序时,我也遇到了同样的问题。我的想法是在应用程序启

我正在使用scoped_模型将状态管理添加到我的聊天应用程序中

我的问题是如何使用具有共享首选项的作用域_模型。因此,应用程序启动模型状态是由共享首选项的值填充的。例如,存储的用户名将从共享首选项中检索,然后存储在UserModel用户名状态中

我已经看过但没有找到教程怎么办

我从电影中找到了这个样本:

这是最好的方法吗? 最好的方法是什么


谢谢

我知道这个问题已经问了很久了。但我还是会回答的

当我使用共享首选项和范围模型开发android应用程序时,我也遇到了同样的问题。我的想法是在应用程序启动时初始化scopedmodel中的值。我这样做的方式是在模型的构造函数中调用函数

假设我的作用域模型是:

class AppModel extends Model {
   String variable1;
   String variable2;
   int variable3;

   //Getter functions go here

   //Setter function (though not a pure setter but a function that sets the values)
   void setValues() {
        SharedPreferences.getInstance().then(
            (prefs) {
                        variable1 = prefs.getString('var1');
                        variable2 = prefs.getString('var2');
                        variable3 = prefs.getInt('var3');
          ));
       }

     AppModel(
           setValues();
      );
}
现在,当您在Material应用程序之前初始化模型时,其值将被初始化

class AppModel extends Model {
   String variable1;
   String variable2;
   int variable3;

   //Getter functions go here

   //Setter function (though not a pure setter but a function that sets the values)
   void setValues() {
        SharedPreferences.getInstance().then(
            (prefs) {
                        variable1 = prefs.getString('var1');
                        variable2 = prefs.getString('var2');
                        variable3 = prefs.getInt('var3');
          ));
       }

     AppModel(
           setValues();
      );
}