简体   繁体   English

dart 变量不等于它们在 object 的构造函数中设置的值

[英]dart variables aren't equal to what they're set to be in the constructor of an object

I have this class called branch:我有这个 class 称为分支:

class Branch {
  String id;
  String businessName;
  String businessAddress;
  String businessPhone;
  int status;

  Branch({
    businessName = "unknown",
    businessAddress = "unknown",
    businessPhone = "unknown",
    id = "unknown",
    status = -1,
  });


Then I have this simple stateful widget:然后我有这个简单的有状态小部件:

class PageMain extends StatefulWidget {

  PageMain({Key key}) : super(key: key);

  @override
  _PageMainState createState() => _PageMainState();
}

class _PageMainState extends State<PageMain> {
  bool _isInitalized = false;    

  void _initializeOnFirstBuild(){
    if(!_isInitialized) {
      Branch testBranch = new Branch(businessName: "test");
      print(testBranch.businessName);
      _isInitialized = true;
    }
  }

  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    _initializeOnFirstBuild();
  }
}

when trying to print the name of the business or any other variable that is stored and set in the constructer it returns null.当尝试打印在构造函数中存储和设置的企业名称或任何其他变量时,它返回 null。 (returns returns "null" instead of "test") (返回返回“null”而不是“test”)

Initialisation in constructor is wrong.构造函数中的初始化是错误的。 You need to use this keyword.您需要使用this关键字。

Branch({
    this.businessName = "unknown",
    this.businessAddress = "unknown",
    this.businessPhone = "unknown",
    this.id = "unknown",
    this.status = -1,
});

Please don't use didChangeDependencies .请不要使用didChangeDependencies

According to the documentation:根据文档:

Called when a dependency of this State object changes.当此 State object 的依赖项发生更改时调用。

And :并且

This method is also called immediately after initState.在 initState 之后也会立即调用此方法。 It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.从此方法调用 BuildContext.dependOnInheritedWidgetOfExactType 是安全的。

https://api.flutter.dev/flutter/widgets/SingleTickerProviderStateMixin/didChangeDependencies.html https://api.flutter.dev/flutter/widgets/SingleTickerProviderStateMixin/didChangeDependencies.html

However, I would not right your business logic in this method, because this method is not guaranteed to always get called, unless any dependencies within the widget change.但是,我不会在此方法中纠正您的业务逻辑,因为不能保证始终调用此方法,除非小部件中的任何依赖项发生更改。 If nothing changes, then this method does not get called.如果没有任何变化,则不会调用此方法。

There are first off, two problems.首先有两个问题。 First you are not overriding the method in the _PageMainState widget.首先,您没有覆盖_PageMainState小部件中的方法。 If you are not overriding the method, then how is _initializeOnFirstBuild supposed to get called if only the base logic runs?如果您没有覆盖该方法,那么如果仅运行基本逻辑,应该如何调用_initializeOnFirstBuild So, add an @override attribute on top of the didChangeDependencies method.因此,在didChangeDependencies方法之上添加一个@override属性。

@override 
void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    _initializeOnFirstBuild();
  }

Then it should work.然后它应该工作。

Second, please instead write your initialisation of the Branch class directly in the initState method, like this:其次,请直接在initState方法中编写分支class 的初始化,如下所示:

 class PageMain extends StatefulWidget {
    
      PageMain({Key key}) : super(key: key);
    
      @override
      _PageMainState createState() => _PageMainState();
    }
    
    class _PageMainState extends State<PageMain> {
      bool _isInitalized = false;    
    
      void _initializeOnFirstBuild(){
        if(!_isInitialized) {
          Branch testBranch = new Branch(businessName: "test");
          print(testBranch.businessName);
          _isInitialized = true;
        }
      }
    
      @override
      void initState() {
        super.initState();
        _initializeOnFirstBuild();
      }
    }

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

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