I have the follwing problem with my code. When I call a Int with SharedPreference then show the emulator the error "_casterror (null check operator used on a null value)". The error is showing only when I start the app.When starting, the value should be _pktH = 0.
...
int? _pktH;
...
void initState() {
super.initState();
_loadCounterH();
}
_loadCounterH() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
_counterH = (prefs.getInt('counterH') ?? 1);
prefs.setInt('counterH', _counterH!);
_pktH = (prefs.getInt('pktH') ?? 0);
prefs.setInt('pktH', _pktH!);
print(Text("Save number is: $_counterH"));
print(Text("Save pkt is: $_pktH"));
});
}
String _abzeichenH;
if (_pktH! >= 2835) {
_abzeichenH = ' ' + ' \u{1F451}' + ' ' + 'Ingenieur/-in';
} else if (_pktH! >= 1890) {
_abzeichenH = ' ' + '\u2692' + '\u2692' + '\u2692' + ' ' + 'Arbeiter/-in';
} else if (_pktH! >= 1260) {
_abzeichenH = ' ' + '\u2692' + '\u2692' + ' ' + 'Azubi';
} else if (_pktH! >= 10) {
_abzeichenH = ' ' + '\u2692' + ' ' + 'Praktikant/-in';
} else {
_abzeichenH = '';
}
print('Dein Abzeichen ist $_abzeichenH');