Flutter 常见异常 NoSuchMethodError: The method 'drive' was called on null.

这是使用回调函数时的常见错误。

/flutter: Another exception was thrown: NoSuchMethodError: The getter 'status' was called on null.
/flutter: Another exception was thrown: NoSuchMethodError: The method 'drive' was called on null.

onTap回调函数参数设置如下:

Container(
  alignment: Alignment.center,
  child: GestureDetector(
    onTap: _register(context),
    child: Text(
      '还没账号?快去注册',
      style: TextStyle(color: Color(0xFF4688FA)),
    ),
  ),
)

//注册
_register(BuildContext context) {
  pushNamed(context, REGISTER_PAGE);
}

解决方案:

_register() {
  pushNamed(context, REGISTER_PAGE);
}

//三种方式
 ListTile(title: Text("Register"), onTap: _register),

 ListTile(title: Text("Register"), onTap: () => _register()),

 ListTile(title: Text("Register"), onTap: () { _register() ; }),

你可能感兴趣的:(Flutter)