-1

I am using getX for fetching data from Firebase.

I used this one before 2.8 update. It was working.

class UserController extends GetxController {
  final Rx<UserModel> _userModel = UserModel(
    email: '',
    name: '',
    uid: '',
    kullaniciAdi: '',
    bildirim: 0,
    userRole: 0,
  ).obs;

  UserModel get user => _userModel.value;

  set user(UserModel value) => _userModel.value = value;

  void clear() {
    _userModel.value = UserModel(
      email: '',
      name: '',
      uid: '',
      kullaniciAdi: '',
      bildirim: 0,
      userRole: 0,
    );
  }
}

I can observ this with GetX or Obx widget. But now, I can't do that anymore.

Here is my Firebase Database code fetching user codes:

Future getUserFromDB(String uid) async {
    try {
      var userData = await _firestore.collection("customers").doc(uid).get();
      var map = userData.data();
      //debugPrint(map!['email']);
      return UserModel.fromData(userData.data());
    } on FirebaseException catch (e) {
      return e.message;
    }
  }

And this is my UserModel:

class UserModel {
  String? uid;
  String? name;
  String? email;
  int? bildirim;
  int? userRole;
  String? kullaniciAdi;
  String? pphoto;

  UserModel({
    this.uid,
    this.name,
    this.email,
    this.bildirim,
    this.userRole,
    this.kullaniciAdi,
    this.pphoto,
  });

  UserModel.fromData(Map<String, dynamic>? dataMap)
      : uid = dataMap!['id'],
        name = dataMap['name'],
        email = dataMap['email'],
        bildirim = dataMap['bildirim'],
        kullaniciAdi = dataMap['kullaniciAdi'];



  Map<String, dynamic> toData() {
    return {
      'uid': uid,
      'name': name,
      'email': email,
      'userRole': userRole,
      'bildirim': bildirim,
      'kullaniciAdi': kullaniciAdi,
      'pphoto': pphoto,
    };
  }
}

I would like to listen this userModel after fetching data. But whenever I use Obx or GetX widget they returns an error. Here is GetX widget and error;

GetX<UserController>(
          initState: (_) async {
            Get.find<UserController>().user = await FirebaseDatabase()
                .getUserData(homeController.userVeri!.uid);
          },
          builder: (_) {
            if (_.user.uid != null) {
              return Text(
                _.user.name.toString(),
                style: const TextStyle(
                    color: Colors.white,
                    fontSize: 30,
                    fontWeight: FontWeight.w500),
              );
            } else {
              return const Text("...");
            }
          },
        ),

Error is:

Null check operator used on nulls value
Tolga Yılmaz
  • 506
  • 1
  • 3
  • 15

1 Answers1

0

It seems to me, that the only possible cause for the error is that you are getting the userVeri!.uid, with a null check(!). Inside the initState builder of your GetX widget. I would suggest first checking whether the userVeri.uid is not null before requesting their data. What I mean is more like this.

GetX<UserController>(
          initState: (_) async {

           if(homeController.userVeri != null){

            Get.find<UserController>().user = await FirebaseDatabase()
                .getUserData(homeController.userVeri!.uid); 
           }
          },
          builder: (_) {
            if (_.user.uid != null && homeController.userVeri != null) {
              return Text(
                _.user.name.toString(),
                style: const TextStyle(
                    color: Colors.white,
                    fontSize: 30,
                    fontWeight: FontWeight.w500),
              );
            } else {
              return const Text("...");
            }
          },
        ),
Rodrick Vy
  • 36
  • 5