Home > OS >  Flutter error: LateError (LateInitializationError: Field 'handler' has not been initialize
Flutter error: LateError (LateInitializationError: Field 'handler' has not been initialize

Time:08-28

I'm trying to receive data from SharedPreferences, but when I first build the app I get this error: LateError (LateInitializationError: Field 'handler' has not been initialized.),

this didn't happen before, but when I first build the app time, it gets stuck.

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();
  }

this is the full code link: https://github.com/carlostic26/Coach_app/blob/master/code.dart

CodePudding user response:

Try to initialize the handler variable

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