How can i check if a firebase realtime database value exists already? (FLUTTER/DART) - flutter

`
Expanded(
child: StreamBuilder(
stream: taskRef != null ? taskRef!.onValue : null,
builder: (context, snapshot) {
if(snapshot.hasData && !snapshot.hasError && data1!="null"){
final snapshot2 = Map<String, dynamic>.from(
((snapshot.data! as DatabaseEvent).snapshot.value??{}) as Map);
if(snapshot2 == null){
return Center(child: Text("No tasks added yet", style: TextStyle(color: Colors.amberAccent,
fontSize: 18),),);
}else{
Map<String, dynamic> map = Map<String, dynamic>.from(snapshot2);
var tasks = <Task>[];
for(var taskMap in map.values){
Task task = Task.fromMap(Map<String, dynamic>.from(taskMap));
tasks.add(task);
}
return ListView.builder(
shrinkWrap: true,
itemCount: tasks.length,
itemBuilder: (context, index){
Task task1 = tasks[index];
return Container`
This is a part of my realtime firebase database...for only one user in this photo.
The code that i used to show the tasks in my app. Should i do the same thing for my problem? The db has userId -> day -> taskId -> informations. When I insert in my DB a new Task (all the information) I want to check first if the startTime already exists. How is this possible? It confuses me because of the random taskId child.
taskRef = FirebaseDatabase.instance.ref().child(uid).child("images").child("MONDAY");

Related

How to delete documents that contain a certain value in one of the fields. Firestore, Flutter

enter image description here
I have these documents, which contain data about each task I add to the list in my app.
child: StreamBuilder(
stream: _tasks.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (streamSnapshot.hasData) {
return ListView.builder(
itemCount: streamSnapshot.data!.docs.length,
itemBuilder: (context, index) {
final DocumentSnapshot documentSnapshot =
streamSnapshot.data!.docs[index];
return GestureDetector(
onLongPress: () => _update(documentSnapshot),
child: ListTile(
)
);
},
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
I am using a stream builder to build the list. Each of the tasks have a checkmark and when i click it, It updates the value in firestore inside the IsDone field accordingly. I want to click a button outside the stream builder to delete the checked tasks. How do I loop through all the documents and find all the documents that contain the value true and delete them?
I tried this but im doing doing something wrong and it isnt changing anything:
void _delete() {
var docs = _tasks.doc().snapshots();
docs.forEach((doc){
if(doc.data()==true){
_tasks.doc(doc.id).delete();
}
});
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('You have successfully deleted a product')));
}
var datas = await FirebaseCalls.firebaseFirestore
.collection("collection_id")
.where("status", isEqualTo: true)
.get();
datas.docs.forEach((element) {
FirebaseFirestore.instance
.collection("collection_id")
.doc(element.id)
.delete();
});
or you can do like this as well.
var datas =
await FirebaseCalls.firebaseFirestore.collection("collection_id").get();
datas.docs
.where((element) => element["status"] == true)
.toList()
.forEach((el) {
FirebaseFirestore.instance
.collection("collection_id")
.doc(el.id)
.delete();
});
You can delete by doing this below:
find the particular document as per your query and get its document and collection ID.
FirebaseFirestore.instance
.collection("collection_id")
.doc("doc_id")
.delete();

firestore doesnt show documents even though they are available

I have following code to add data to firebasefirestore
Future<void> sendMessage({
required String msg,
required String id,
}) async {
var docId = getDocId(id); // returns sth like "AbcDe-FghiJ"
DocumentReference documentReferencer = chat.doc(docId).collection('chatMsg').doc();
Map<String, dynamic> data = <String, dynamic>{
"message": msg,
"sentBy": ownId,
"sentAt": DateFormat('yyyy-MM-dd – kk:mm:ss').format(DateTime.now())
};
await documentReferencer.set(data);
}
I used following code to get the data
StreamBuilder<QuerySnapshot>(
stream: firebaseInstance.collection('Messages').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError || !snapshot.hasData) {
return const Center(
child: CircularProgressIndicator()
);
} else {
var data = snapshot.data.docs;
return listBuilder(data);
}
}
)
listBuilder(listData) {
return ListView.builder(
shrinkWrap: true,
itemCount: listData.length,
itemBuilder: (BuildContext context, int index) {
return Text(listData[index].id);
}
)
}
However, data show 0 items even though there is a document present.
My question is how can I get the list of documents from Messages?
I was having the same exact problem with subcollections on Firestore and even asked a question here to get some help over it. Though, it seems like the snapshots won't show the documents having a subcollection in them as there is no field inside any of them. So what I did to counter this was to just add anything (just a random variable) and then it was able to find the documents.
This is my current layout:
I've just added another line of code to just add this whenever I'm inserting a new subcollection.
collection
.set({
'dummy': 'data'
})
.then((_) => print('Added'))
.catchError((error) => print('Add failed: $error'));

Firebase Realtime Database Ordering by Keys

I want to sort data in Firebase realtime database.
I am using timestamp as key when saving data and I want to sort data by timestamps. I used below code for this purpose.
Widget buildList(ChatUser chatUser) {
return Flexible(
child: StreamBuilder(
stream: _service
.getMessages(chatUser.uid!)
.orderByKey()
.onValue,
builder: (context, snapshot) {
List<ChatMessage> messageList = [];
if (snapshot.hasData) {
final myMessages = Map<dynamic, dynamic>.from(
(snapshot.data as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
myMessages.forEach((key, value) {
final currentMessage = Map<String, dynamic>.from(value);
final message = ChatMessage().fromJson(currentMessage);
messageList.add(message);
});
if (messageList.isNotEmpty) {
return ListView.builder(
padding: const EdgeInsets.all(10),
reverse: true,
itemCount: messageList.length,
controller: scrollController,
itemBuilder: (context, index) {
return buildItem(index, messageList[index], chatUser);
});
} else {
return const Center(
child: Text('Henüz Mesaj yok.'),
);
}
} else {
return const Center(
child: CircularProgressIndicator(
color: Colors.red,
),
);
}
}));
}
As a result, data does not come according to key values, it comes in different orders.
Any suggestions ? Thanks.
The problem is in how you process the results here:
if (snapshot.hasData) {
final myMessages = Map<dynamic, dynamic>.from(
(snapshot.data as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
myMessages.forEach((key, value) {
final currentMessage = Map<String, dynamic>.from(value);
final message = ChatMessage().fromJson(currentMessage);
messageList.add(message);
});
The order of the keys inside a Map is by definition undefined. So when you call (snapshot.data as DatabaseEvent).snapshot.value as Map<dynamic, dynamic>), you're actually dropping all ordering information that the database returns.
To process the results in the correct order, iterate over the children of the snapshot, and only then convert each child to a Map.
Complementing Frank, try to assign snapshot to a List of snapshots using List snapshotList = xxx.children.toList(); If you do something like snapshotList[i].value you will notice that the key is not present, the solution to get it back is to use the get .key.
You can see bellow an exemple how I did to solve the same problem in my project.
final List<DataSnapshot> snapshotList = snapshot.data.children.toList();
final List<Map> commentsList = [];
for (var i in snapshotList) {
Map<String?, Map> comment = {i.key: i.value as Map};
commentsList.add(comment);
}
In the code above, commentsList will get you a list of Maps ordered according to original database.
I hope this help. If anyone has a more straightforward solution, please, share with us.

How can I get data from various nodes in a Realtime-database in Flutter?

I'm trying to display data from a real-time database in my widget, such as a picture, a name, or a message, but I'm not sure how to achieve it from several nodes. Thank you in advance for your assistance.
For add data :
List lists = [];
stream to get data :
final dbRef = FirebaseDatabase.instance
.ref()
.child("chatList")
.child("D1NilPUI6PY0jSA1tk0wRzi6FsO2");
Widget to show data :
_widget() {
return StreamBuilder(
stream: dbRef.onValue,
builder: (BuildContext context, AsyncSnapshot snap) {
if (snap.hasData &&
!snap.hasError &&
snap.data!.snapshot.value != null) {
Map data = snap.data.snapshot.value;
List item = [];
data.forEach((index, data) => item.add({"chatList": index, ...data}));
print("DATA : $item");
if (snap.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: item.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(item[index]['content'].toString()),
);
},
);
}
} else {
return const Center(child: Text("No data"));
}
},
);
}
Table structure Images :
Image 1 :
Image 2 :
This code:
final dbRef = FirebaseDatabase.instance
.ref()
.child("chatList")
.child("D1NilPUI6PY0jSA1tk0wRzi6FsO2");
This refers to a node /chatList/D1NilPUI6PY0jSA1tk0wRzi6FsO2 in your database. Since the screenshot doesn't show any data under that exact path, you will get a snapshot without any value from reading it.
If you want to read all nodes under /chatList, you can use that path in the query, and then loop over all the children of the snapshot.
final dbRef = FirebaseDatabase.instance
.ref()
.child("chatList");
dbRef.onValue.listen((event) => {
event.snapshot.children.forEach((child) {
print(child.key);
})
})
Since you have two levels with dynamic keys under chatList, you'll have to use two nested loops to get to the named properties:
final dbRef = FirebaseDatabase.instance
.ref()
.child("chatList");
dbRef.onValue.listen((event) => {
event.snapshot.children.forEach((child) {
print(child.key);
child.children.forEach((child2) {
print(child2.key);
print(child2.child("lastMessage/content").value);
})
})
})

convert data from Firebase RTDB

I'm expecting my random value to be an int but I got a String instead,
is this the right way to do it.
StreamBuilder(
stream: FirebaseDatabase.instance.ref().child('RandomVal').onValue,
builder: (context, snapshot) {
if (snapshot.hasData && !snapshot.hasError) {
final event = snapshot.data as DatabaseEvent;
final data = event.snapshot.value as Map;
print(data['Value']); // my value as expected
print(data['Value'].runtimeType); // String instead of int
}
return Text('please wait');
},
),