简体   繁体   English

如何删除scoped_model状态?

[英]How to delete scoped_model state?

I am use scoped_model for handle app state but I need to wipe all stored state (for example on user sign out). 我正在使用scoped_model处理应用程序状态,但是我需要擦除所有存储的状态(例如,在用户注销时)。

How to delete all state store in scoped_model ? 如何删除scoped_model所有状态存储?

I know can just set state value to null. 我知道可以将状态值设置为null。 For example: 例如:

var = null

But this difficult to maintain if add new variable to state in scoped_model . 但是,如果在scoped_model添加新变量到状态,则很难维护。

I am look for easy way to do. 我正在寻找简单的方法。

UPDATE: 更新:

Example code store state: 示例代码存储状态:

First declare bool: bool isUserRegister; 首先声明bool: bool isUserRegister;

Then set isUserRegister = true; 然后设置isUserRegister = true; after check database. 之后检查数据库。

I have many of this type store in scoped_model . 我在scoped_model有许多此类存储。 I need simple way to delete all state. 我需要简单的方法来删除所有状态。

I am initialize model at top of widget hierarchy and wrap MaterialApp in ScopedModel : 我在小部件层次结构的顶部初始化模型,并将MaterialApp包装在ScopedModel

void main() {
  runApp(new ExampleApp());
}

class ExampleApp extends StatelessWidget {

    final ExampleModel exampleModel = ExampleModel();

    //...

        return ScopedModel<ExampleModel>(
            model: exampleModel,
            child: MaterialApp(

Maybe one way is to initialize the model again? 也许一种方法是再次初始化模型?

You can create a method in your state class (the one that extends the Model class) that sets all your objects to null/empty arrays. 您可以在状态类(扩展Model类的方法)中创建一个将所有对象设置为null / empty数组的方法。 It's hard to provide a certain answer given that you didn't provide any code to us. 鉴于您没有向我们提供任何代码,很难提供一个肯定的答案。


Let's say that's your model: 假设这是您的模型:

class ExampleModel extends Model {
  String _someString = "some value";
  bool _someBool = false;

  void clear() {
    _someState = null;
    _someBool = null;
  }
}

You can just create a method like clear() and call it whenever you like. 您可以创建像clear()这样的方法,并在需要时调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM