我有一个按钮,我想在增加我的计数器后禁用它。这是我用来增加计数器的方法:
void incrementAdCounter() async {
setState(() {
adCounter++;
if (adCounter == 2 || adCounter > 2) {
isAdButtonDisabled = true;
}
setAdCounter();
print(adCounter);
});
}
在 initState 中:
void initButtons() {
isAdButtonDisabled = false;
adCounter = 0
}
我有一个按钮,如果按钮,我在 onTap 上调用了这些方法:
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter');
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}
setAdCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('adCounter', adCounter);
prefs.setBool('isAdButtonDisabled', isAdButtonDisabled);
}
当我调用此方法时,它向我显示以下错误:
Unhandled Exception: NoSuchMethodError: The method '+' was called on null.
E/flutter (13166): Receiver: null
试试这个,
void getCounters() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
adCounter = prefs.getInt('adCounter') ?? 0;
isAdButtonDisabled = prefs.getBool('isAdButtonDisabled');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句