Reputation: 177
I'm using a Firestore database to store information about users connected to a session in my app. To leave, users press a leave button which then is supposed to delete their own data from the database. Here's my code for deleting a client:
_deleteAClientFromDB() async {
//get specific user in users
var userLobby = await users
.where('lobbyCode', isEqualTo: _lobbyCode)
.where('userRole', isEqualTo: 2)
.where('userID', isEqualTo: _userID)
.get();
userLobby.docs.forEach((document) {
document.reference.delete();
});
}
this code is very similar to my function to delete all users from the database with the added "where" of the user ID. The userID
is a string field that is unique per user. However, I'm pretty sure what's happening is that when I try to run this function on a device, it somehow runs the function on all devices? That's the only reason I can think of since if userID
is not the same as the one in firestore it wouldn't be retrieved. I initialize with this function to extract my SharedPreferences
:
Future<String> _getData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(
() {
_lobbyCode = (prefs.getString('lobbyCode'));
_userID = (prefs.getString('userID'));
_isHost = prefs.getBool('isHost') ?? false;
},
);
I'm not sure why the function is deleting all documents with the userRole
of 2 while not only deleting the document with the userID
of _userID
(locally stored data). Does anyone understand what it is that I'm doing wrong?
E/flutter ( 8795): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Invalid argument(s)
E/flutter ( 8795): #0 _StringBase.+ (dart:core-patch/string_patch.dart:267:57)
E/flutter ( 8795): #1 _LobbyWidgetState._deleteAClientFromDB (package:percepto/lobby.dart:350:46)
E/flutter ( 8795): #2 _LobbyWidgetState._showClientConfirmDialog.<anonymous closure> (package:percepto/lobby.dart:488:9)
It seems like there really is something wrong with _userID
, I tried to do a log of log("This is what userID is:" + _userID);
and was met with these errors. I'm not sure why it could be an invalid argument because it was initialized as an empty string and set from the device's local storage.
Upvotes: 0
Views: 62
Reputation: 177
Thanks for recommending me to check _userID, after I saw the log error I double checked where I set the shared_preferences
and apparently I named userID
id
. So I was trying to extract data from a prefs that didn't exist.
Upvotes: 1