Home > Software engineering >  Flutter Future<Object> not return
Flutter Future<Object> not return

Time:03-06

Becoming mad about a simple piece of code...

I have a class:

class Monkey {
  final String uid;
  final String email;
  final String password;
  final int registrationMethod;
  final int accountCreationTime; //* in milliseconds
  final String name;
  final String nickname;
  final int birthday; //* in milliseconds
  final String phone;
  final String countryCode;
  final String codiceComune;
  final String comune;
  final String provincia;
  final String regione;
  final String cap;
  final String sigla;
  final String zona;
  final String codiceCatastale;
  final int popolazione;
  final bool isRegistered;
  final bool shareName;
  final bool acceptedRules;
  final String picUrl;
  final int
      userPicType; //* 1 - Email / 2 - Google / 3 - Facebook / 4 - Apple / 5 - File

  const Monkey(
    this.uid,
    this.email,
    this.password,
    this.registrationMethod,
    this.accountCreationTime,
    this.name,
    this.nickname,
    this.birthday,
    this.phone,
    this.countryCode,
    this.codiceComune,
    this.comune,
    this.provincia,
    this.regione,
    this.cap,
    this.sigla,
    this.zona,
    this.codiceCatastale,
    this.popolazione,
    this.isRegistered,
    this.shareName,
    this.acceptedRules,
    this.picUrl,
    this.userPicType,
  );
}

I have a variable:

  static Monkey monkey = const Monkey(
      'uid',
      'email',
      'password',
      0,
      0,
      'name',
      'nickname',
      0,
      'phone',
      'countryCode',
      'codiceComune',
      'comune',
      'provincia',
      'regione',
      'cap',
      'sigla',
      'zona',
      'codiceCatastale',
      0,
      false,
      false,
      false,
      'picUrl',
      0);

I have a method to fill the variable:

Future<Monkey?> getMonkey() async {
    try {
      await _firestore
          .collection('users')
          .doc(getUID())
          .get()
          .then((DocumentSnapshot documentSnapshot) {
        Map<String, dynamic> data =
            documentSnapshot.data() as Map<String, dynamic>;
        if (data.isNotEmpty) {
          if (kDebugMode) {
            print("Point 1");
          }
          return Monkey(
              data['uid'],
              data['email'],
              data['password'],
              data['registrationMethod'],
              data['accountCreationTime'],
              data['name'],
              data['nickname'],
              data['birthday'],
              data['phone'],
              data['countryCode'],
              data['codiceComune'],
              data['comune'],
              data['provincia'],
              data['regione'],
              data['cap'],
              data['sigla'],
              data['zona'],
              data['codiceCatastale'],
              data['popolazione'],
              data['isRegistered'],
              data['shareName'],
              data['acceptedRules'],
              data['picUrl'],
              data['userPicType']);
        } else {
          if (kDebugMode) {
            print(
                "La lettura di monkey non ha restituito dati, elsedella lettura Monkey.");
          }
          return null;
        }
      });
    } catch (err) {
      if (kDebugMode) {
        print("Errore durante la lettura di Monke: "   err.toString());
      }
      return null;
    }
    if (kDebugMode) {
      print(
          "Point 2");
    }
    return null;
  }

This is my execution:

Variables.monkey = (await _firebase.getMonkey())!;

As I can see my code fire Print Point 1 but also Print Point 2. I printed data and it is filled successfully. But I receive this error:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value #0 _RegistrationScreenState.registerUser package:whybye/screens/registration_screen.dart:345

edit: Just removed any 'final' from Class Monkey, however same result.

CodePudding user response:

The error is here:

Future<Monkey?> getMonkey() async {
    try {
      await _firestore
          .collection('users')
          .doc(getUID())
          .get()
          .then((DocumentSnapshot documentSnapshot) {
        Map<String, dynamic> data =
            documentSnapshot.data() as Map<String, dynamic>;
        if (data.isNotEmpty) {
          if (kDebugMode) {
            print("Point 1");
          }
          return Monkey(
              data['uid'],
              data['email'],
              data['password'],
              data['registrationMethod'],
              data['accountCreationTime'],
              data['name'],
              data['nickname'],
              data['birthday'],
              data['phone'],
              data['countryCode'],
              data['codiceComune'],
              data['comune'],
              data['provincia'],
              data['regione'],
              data['cap'],
              data['sigla'],
              data['zona'],
              data['codiceCatastale'],
              data['popolazione'],
              data['isRegistered'],
              data['shareName'],
              data['acceptedRules'],
              data['picUrl'],
              data['userPicType']);
        } else {
          if (kDebugMode) {
            print(
                "La lettura di monkey non ha restituito dati, elsedella lettura Monkey.");
          }
          return null;
        }
      });
    } catch (err) {
      if (kDebugMode) {
        print("Errore durante la lettura di Monke: "   err.toString());
      }
      return null;
    }
    if (kDebugMode) {
      print(
          "Point 2");
    }
    return null;
  }

Where return Monkey is inside another process (await _firestore). This mean the code is ok but I had to add:

Monkey monkey;
monkey = await _firestore...

return monkey(...)...

and outside the _firestore process return the real monkey...

Before I found the solution I tried tons of code and finally I switched to String:

  Future<String> getMonkey() async {
    String res;
    try {
      res = await _firestore
          .collection('users')
          .doc(getUID())
          .get()
          .then((DocumentSnapshot documentSnapshot) {
        Map<String, dynamic> data =
            documentSnapshot.data() as Map<String, dynamic>;
        if (data.isNotEmpty) {
          if (kDebugMode) {
            print("I dati di Monkey non sono vuoti: ");
          }
          Monkey.uid = data['uid'];
          Monkey.email = data['email'];
          Monkey.password = data['password'];
          Monkey.registrationMethod = data['registrationMethod'];
          Monkey.accountCreationTime = data['accountCreationTime'];
          Monkey.name = data['name'];
          Monkey.nickname = data['nickname'];
          Monkey.birthday = data['birthday'];
          Monkey.phone = data['phone'];
          Monkey.countryCode = data['countryCode'];
          Monkey.codiceComune = data['codiceComune'];
          Monkey.comune = data['comune'];
          Monkey.provincia = data['provincia'];
          Monkey.regione = data['regione'];
          Monkey.cap = data['cap'];
          Monkey.sigla = data['sigla'];
          Monkey.zona = data['zona'];
          Monkey.codiceCatastale = data['codiceCatastale'];
          Monkey.popolazione = data['popolazione'];
          Monkey.isRegistered = data['isRegistered'];
          Monkey.shareName = data['shareName'];
          Monkey.acceptedRules = data['acceptedRules'];
          Monkey.picUrl = data['picUrl'];
          Monkey.userPicType = data['userPicType'];
          return 'success';
        } else {
          if (kDebugMode) {
            print(
                "La lettura di monkey non ha restituito dati, elsedella lettura Monkey.");
          }
          return 'error';
        }
      });
    } catch (err) {
      if (kDebugMode) {
        print("Errore durante la lettura di Monkey: "   err.toString());
      }
      return 'error';
    }
    if (res == 'success') {
      return 'success';
    } else {
      return 'error';
    }
  }

As you can see the real Return function is at bottom:

if (res == 'success') {
          return 'success';
        } else {
          return 'error';
        }

CodePudding user response:

This error occurs when you use a bang operator (!) on a nullable instance which wasn't initialized.

Replace your execution with this:

var monkey = await _firebase.getMonkey();
if(monkey != null){
  Variables.monkey = monkey;
}

I hope it would help you.

  • Related