'Using RxList variable and trigger other RxList variable on GetxController streamBinding() with firebase
I am using Firebase Firestore. and also GetxController, RxList, Streaming
First of all, I would like to describe my scenario roughly. In my scenario I am adding or removing the Clubs array in the player collection. This change I made is correctly reflected in the AccountController.clubs array variable. but the list from the readClubs method, where I pulled the details of the club information, is not updated. I am using the AccountController.clubs array list in the where condition in the readClubs() method. Although the array list is updated, the _clubs RxList variable and ClubController.clubs object list, where I keep the detailed club list, are not updated. I can see the correct values only when I restart the application I have shared the relevant branches below. I haven't shared the view codes because I can't see the correct values in the variables yet.
Data Model
//Players:
[
{
"playerUid":"1",
"Name":"Ramazan",
"Clubs":["1","2"]
}
]
//Clubs:
[
{
"clubUid":"1",
"Name":"Club1"
},
{
"clubUid":"2",
"Name":"Club2",
},
{
"clubUid":"3",
"Name":"Club3"
}
]
AccountController
onInit() async {
super.onInit();
_loggedAccount.bindStream(Database.initAccount(uid));
}
final Rx<AccountModel> _loggedAccount = AccountModel().obs;
List<String> get clubs => _getPlayerCLubsUids();
AccountModel get loggedAccount => _loggedAccount.value;
List<String> _getPlayerCLubsUids() {
List<String> clubUids = [];
if (loggedAccount.clubs != null) {
for (var e in loggedAccount.clubs!) {
clubUids.add(e.uid);
}
}
return clubUids;
}
ClubController
final RxList<ClubModel> _clubs = <ClubModel>[].obs;
List<ClubModel> get clubs => _getCLubs();
@override
void onInit() {
super.onInit();
_clubs.bindStream(Database.readClubs());
}
Database
static Stream<List<ClubModel>> readClubs() {
if (AccountController.to.clubs.isNotEmpty) {
return _firestore
.collection('clubs')
.where('uid', whereIn: AccountController.to.clubs)
.snapshots()
.map((QuerySnapshot query) {
List<ClubModel> retVal = [];
for (var element in query.docs) {
retVal.add(ClubModel.fromSnapshot(element));
}
return retVal;
});
} else {
return const Stream.empty();
}
}
The part I think is causing the problem. in Database.read Clubs() because I have same matzot without where condition.
.where('uid', whereIn: AccountController.to.clubs)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
