颤振错误:延迟错误(延迟初始化错误:字段"处理程序"尚未初始化。



我正试图从SharedPreferences接收数据,但当我第一次构建应用程序时,我收到了以下错误:LateError (LateInitializationError: Field 'handler' has not been initialized.)

这种情况以前从未发生过,但当我第一次构建应用程序时,它就陷入了困境。

class razonamiento extends StatefulWidget {
const razonamiento({Key? key}) : super(key: key);
@override
State<razonamiento> createState() => _razonamientoState();
}
class _razonamientoState extends State<razonamiento> {
// here shows error: LateError (LateInitializationError: Field 'handler' has not been initialized.)
late DatabaseHandler handler; 
int scoreTotal = 0;
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
Future<List<scoregamilibre>>? _scoreRC;
String urlImagen = '';
String carrera = '';
String nombre = '';
@override
void initState() {
if (LocalStorage.prefs.getBool('firstLog') == true) {
//getting avatar url img
if (LocalStorage.prefs.getString('url') != '') {
var url = LocalStorage.prefs.getString('url')?.length ??
'http://gamilibre.com/imagenes/user.png';
urlImagen = url.toString();
LocalStorage.prefs.setBool("firstLog", false);
} else {
urlImagen = "http://gamilibre.com/imagenes/user.png";
}
} else {
if (LocalStorage.prefs.getString('url') != null) {
var test = LocalStorage.prefs.getString('url');
urlImagen = test.toString();
} else {
urlImagen = 'http://gamilibre.com/imagenes/user.png';
}
}
//receiving carera
if (LocalStorage.prefs.getString('carrera') != '') {
carrera = LocalStorage.prefs.getString('carrera')!;
} 
_scoreRC = getScoresRC();
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
loggedInUser = UserModel.fromMap(value.data());
setState(() {});
});
super.initState();
}
//get the list with all rows from table score
Future<List<scoregamilibre>> getScoresRC() async {

//here handler is used
return await handler.QueryAllScoresRC();
}

这是完整的代码链接:https://github.com/carlostic26/Coach_app/blob/master/code.dart

尝试初始化handler变量

@override
void initState() {
handler  = DatabaseHandler ();

最新更新