Android 方法'~/';在空颤振时被调用

Android 方法'~/';在空颤振时被调用,android,flutter,mobile,flutter-layout,Android,Flutter,Mobile,Flutter Layout,当我调用我的倒计时小部件时,出现以下异常: 建筑倒计时(脏,状态:_CountdownState#97b4a)引发了以下NoSuchMethodError错误: 对null调用了方法“~/”。 收件人:空 已尝试呼叫:~/(3600) 导致错误的小部件的用户创建的祖先为 容器 初始化状态为空 1毫秒后,我的倒计时打印在应用程序栏中,效果很好,但为什么 class IneatAppBarPollWidget { getAppBar(String title, String logo, Cand

当我调用我的倒计时小部件时,出现以下异常:

建筑倒计时(脏,状态:_CountdownState#97b4a)引发了以下NoSuchMethodError错误: 对null调用了方法“~/”。 收件人:空 已尝试呼叫:~/(3600)

导致错误的小部件的用户创建的祖先为 容器

初始化状态为空

1毫秒后,我的倒计时打印在应用程序栏中,效果很好,但为什么

class IneatAppBarPollWidget {
  getAppBar(String title, String logo, Candidate candidate) {
    return AppBar(
      title: Text(title),
      leading: Image.asset(
        logo,
        fit: BoxFit.contain,
        height: 32,
      ),
      actions: <Widget>[
        new Container(
          child: new Row(
            children: <Widget>[
              new Container(
                margin:EdgeInsets.only(right: 10),
                child: new Countdown(),
              ),
              new CircleAvatar(
                child: new Text(
                  '${candidate.firstname[0].toUpperCase()}.${candidate.lastname[0].toUpperCase()}',
                  style: TextStyle(fontSize: 20),
                ),
                backgroundColor: Colors.white,
              ),
            ],
          ),
          margin: EdgeInsets.only(right: 10),
        ),
      ],
    );
  }
}
类IneAppBarPollWidget{
getAppBar(字符串标题、字符串徽标、候选项){
返回AppBar(
标题:文本(标题),
领先:Image.asset(
标志
适合:BoxFit.contain,
身高:32,
),
行动:[
新容器(
孩子:新的一排(
儿童:[
新容器(
页边距:仅限边集(右:10),
孩子:新倒计时(),
),
新圆星(
儿童:新文本(
“${candidate.firstname[0].toUpperCase()}.${candidate.lastname[0].toUpperCase()}”,
样式:TextStyle(字体大小:20),
),
背景颜色:Colors.white,
),
],
),
页边距:仅限边集(右:10),
),
],
);
}
}

类倒计时扩展StatefulWidget{
@凌驾
_CountdownState createState()=>\u CountdownState();
}
类_CountdownState扩展状态{
定时器(u定时器),;
整数秒;
@凌驾
小部件构建(构建上下文){
返回中心(
子:文本(
施工时间(秒),
样式:TextStyle(
fontSize:18,颜色:Colors.white,fontStyle:fontStyle.italic),
),
);
}
字符串构造时间(整数秒){
整小时=秒~/3600;
整数分钟=秒%3600~/60;
整数秒=秒%60;
返回时间(小时)+
":" +
格式化时间(分钟)+
":" +
形成时间(秒);
}
字符串格式时间(int-timeNum){
返回timeNum<10?“0”+timeNum.toString():timeNum.toString();
}
@凌驾
initState(){
getDate();
startTimer();
super.initState();
}
void getDate()异步{
var now=DateTime.now();
字符串creationDate=await Poll().getCreationDateToSF();
DateTime creationDateTime=DateTime.parse(creationDate);
秒=creationDateTime.difference(现在)。Unseconds;
}
void startTimer(){
常数周期=常数持续时间(秒:1);
_定时器=定时器。周期(周期,(定时器){
设置状态(){
秒--;
});
如果(秒==0){
取消计时器();
}
});
}
void cancelTimer(){
如果(_timer!=null){
_timer.cancel();
_定时器=空;
}
}
@凌驾
无效处置(){
super.dispose();
取消计时器();
}
}

在设置
秒之前呈现布局。您应该为
指定一个初始值

int seconds = 0;

如果你不给我们看你的倒计时()小部件,我不知道我们在这里会有什么帮助。@Marc我编辑了我的帖子,抱歉,只是一个猜测。。。这可能是秒或计时器实例变量的初始化问题?好的,可能。。。但是为什么1ms后我会在appbar中显示结果@Marc错误发生在带有creationDate字符串的getDate方法上,但为什么@马可:谢谢你!现在我没有任何错误,但我在倒计时前第一次打印了0?我可以解决它吗?您可以在
constructTime
方法的开头添加对0的检查:
if(seconds==0)return“”好的,很好,我总是在打印倒计时前1毫秒,但是很好,谢谢
int seconds = 0;