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