In my Flutter app, there is a LateInitializationError at first but after a second the error is no longer there. Here is a small gif showing the issue: https://i.stack.imgur.com/htJXC.gif
This is the full error it gives:
======== Exception caught by widgets library =======================================================
The following LateError was thrown building HomePage(dirty, state: HomePageState#cbe92):
LateInitializationError: Field 'ilkSayiOynama' has not been initialized.
The relevant error-causing widget was:
HomePage file:///C:/Users/emirs/AndroidStudioProjects/tahmin_oyunu/lib/main.dart:56:15
When the exception was thrown, this was the stack:
#0 ilkSayiOynama (package:tahmin_oyunu/home.dart)
#1 HomePageState.build (package:tahmin_oyunu/home.dart:168:83)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
My initState():
@override
void initState() {
checkIfEmpty();
_bannerAd = BannerAd(
adUnitId: "ca-app-pub-/------",
request: AdRequest(),
size: AdSize.banner,
listener: BannerAdListener(
onAdFailedToLoad: (ad, err) {
print('Failed to load a banner ad: ${err.message}');
ad.dispose();
},
),
);
_bannerAd.load();
super.initState();
}
The checkIfEmpty
function:
checkIfEmpty() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var list = prefs.getString("bilgiler");
if (list == null) {
var bilgiler = {
"para": 150,
"anahtar": false,
"ilkSayiOynama": 10,
"ikinciSayiOynama": 10,
"ucuncuSayiOynama": 50,
"ilkSayiKatOran2": 0,
"ikinciSayiKatOran2": 0,
"ucuncuSayiKatOran2": 0,
"ilkSayiparaKaybetme": 0,
"ikinciSayiparaKaybetme": 0,
"ucuncuSayiparaKaybetme": 0,
"anahtarYazi": "Satın Alınmadı"
};
prefs.setString("bilgiler", jsonEncode(bilgiler));
para = 150;
ilkSayiOynama = 10;
ikinciSayiOynama = 10;
ucuncuSayiOynama = 50;
anahtar = false;
anahtarYazi = "Satın Alınmadı";
ilkSayiKatOran2 = 0;
ikinciSayiKatOran2 = 0;
ucuncuSayiKatOran2 = 0;
ilkSayiparaKaybetme = 0;
ikinciSayiparaKaybetme = 0;
ucuncuSayiparaKaybetme = 0;
setState(() {});
} else {
var listJson = jsonDecode(list);
para = listJson["para"];
ilkSayiOynama = listJson["ilkSayiOynama"];
ikinciSayiOynama = listJson["ikinciSayiOynama"];
ucuncuSayiOynama = listJson["ucuncuSayiOynama"];
anahtar = listJson["anahtar"];
anahtarYazi = listJson["anahtarYazi"];
ilkSayiKatOran2 = listJson["ilkSayiKatOran2"];
ikinciSayiKatOran2 = listJson["ikinciSayiKatOran2"];
ucuncuSayiKatOran2 = listJson["ucuncuSayiKatOran2"];
ilkSayiparaKaybetme = listJson["ilkSayiparaKaybetme"];
ikinciSayiparaKaybetme = listJson["ikinciSayiparaKaybetme"];
ucuncuSayiparaKaybetme = listJson["ucuncuSayiparaKaybetme"];
setState(() {});
}
}
My build
function:
@override
Widget build(BuildContext context) {
Functions functions = Functions();
return Scaffold(
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: SingleChildScrollView(
child: Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(15, 15, 15, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FittedBox(
fit: BoxFit.fitWidth,
child: Container(
width: 117,
child: Column(
children: [
TextField(
controller: s1,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: "1.Sayı",
helperText: "1 - 3 Arasında \nOynama Hakkı : $ilkSayiOynama\n$s1Tahmin",
),
),
],
),
),
),
FittedBox(
fit: BoxFit.fitWidth,
child: Container(
width: 117,
child: Column(
children: [
TextField(
controller: s2,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: "2.Sayı",
helperText: "1 - 5 Arasında \nOynama Hakkı : $ikinciSayiOynama\n$s2Tahmin",
),
),
],
),
),
),
FittedBox(
fit: BoxFit.fitWidth,
child: Container(
width: 117,
child: Column(
children: [
TextField(
controller: s3,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: "3.Sayı",
helperText: "1 - 7 Arasında \nOynama Hakkı : $ucuncuSayiOynama\n$s3Tahmin",
),
),
],
),
) ,
)
],
),
),
Padding(
padding: EdgeInsets.only(top: 45),
child: Container(
width: 100,
child: Column(
children: [
TextField(
controller: oran,
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: "Oran"),
),
TextField(
controller: miktar,
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: "Miktar"),
)
],
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(25,25,0,0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Para : $para", style: TextStyle(fontSize: 20)),
],
),
),
Padding(
padding: EdgeInsets.only(top: 10),
child:ElevatedButton(
onPressed: () {
functions.oynaButton(context);
setState(() {});
},
child: Text("Oyna")),
),
Padding(
padding: EdgeInsets.only(top: 25),
child: Column(
children: [
Text("Satın Alınanlar", style: TextStyle(fontSize: 27),),
Padding(padding: EdgeInsets.fromLTRB(10,13,0,0),
child: Column(
children: [
Container(
child: Text("3.Sayi 2X Oran : $ucuncuSayiKatOran2",
style: TextStyle(fontSize: 16),
),
),
Container(
padding: EdgeInsets.only(top: 11),
child: Text("3.Sayi 2X Oran : $ucuncuSayiKatOran2",
style: TextStyle(fontSize: 16),
),
),
Container(
padding: EdgeInsets.only(top: 11),
child: Text("3.Sayi 2X Oran : $ucuncuSayiKatOran2",
style: TextStyle(fontSize: 16),
),
),
Container(
padding: EdgeInsets.only(top: 11),
child: Text("1.Sayı Para Kaybetmeme : $ilkSayiparaKaybetme", style: TextStyle(fontSize: 16)),
),
Container(
padding: EdgeInsets.only(top: 11),
child: Text("2.Sayı Para Kaybetmeme : $ikinciSayiparaKaybetme", style: TextStyle(fontSize: 16)),
),
Container(
padding: EdgeInsets.only(top: 11),
child: Text("3.Sayı Para Kaybetmeme : $ucuncuSayiparaKaybetme", style: TextStyle(fontSize: 16)),
),
Container(
padding: EdgeInsets.only(top: 11),
child:Text("Anahtar : $anahtarYazi \n", style: TextStyle(fontSize: 16),),
),
],
),
) ,
],
),
),
Padding(
padding: EdgeInsets.only(bottom: 10),
child: Align(
alignment: Alignment.topCenter,
child: Container(
width: _bannerAd.size.width.toDouble(),
height: _bannerAd.size.height.toDouble(),
child: AdWidget(ad: _bannerAd),
),
),
),
Padding(
padding: EdgeInsets.only(right: 15, bottom: 10),
child: Align(
alignment: Alignment.bottomRight,
child: ElevatedButton(onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Emin misin?"),
content: Text("Bu tüm değerlerini sıfırlar!"),
actions: [
TextButton(
child: Text("Hayır"),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
child: Text("Evet"),
onPressed: () async {
var prefs = await SharedPreferences.getInstance();
var list = prefs.getString("bilgiler");
var bilgiler = jsonDecode(list!);
bilgiler = {
"para": 150,
"anahtar": bilgiler["anahtar"],
"ilkSayiOynama": 10,
"ikinciSayiOynama": 10,
"ucuncuSayiOynama": 50,
"ilkSayiKatOran2": 0,
"ikinciSayiKatOran2": 0,
"ucuncuSayiKatOran2": 0,
"ilkSayiparaKaybetme": 0,
"ikinciSayiparaKaybetme": 0,
"ucuncuSayiparaKaybetme": 0,
"anahtarYazi": bilgiler["anahtarYazi"]
};
prefs.setString("bilgiler", jsonEncode(bilgiler));
para = 150;
ilkSayiOynama = 10;
ikinciSayiOynama = 10;
ucuncuSayiOynama = 50;
ilkSayiKatOran2 = 0;
ikinciSayiKatOran2 = 0;
ucuncuSayiKatOran2 = 0;
ilkSayiparaKaybetme = 0;
ikinciSayiparaKaybetme = 0;
ucuncuSayiparaKaybetme = 0;
setState(() {});
functions.showSnackBar(context, "Değerleriniz sıfırlandı");
Navigator.pop(context);
},
)
],
);
},
);
},
child: Text("Sıfırla") ,
),
),
),
],
),
),
),
),
);
}
As you can see, ilkSayiOynama
gets initialized no matter what. The problem is that it shows the error, and then the error is gone and I can use ilkSayiOynama
without any problems. I put the checkIfEmpty();
function before super.initState()
but that did not work either. I'm thinking that the build
function is trying to access ilkSayiOynama
before its initialized. How can I stop this from happening?