I am following a course to create a messaging application using flutter, and I followed all the steps to get the messages to Cloud Firestore, but in the end I do not receive anything.
this is the code:-
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
static const String screenRoute = "chat_screen";
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final _fireStore = FirebaseFirestore.instance;
final _auth = FirebaseAuth.instance;
late User signInUser;
String? messageText;
@override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) {
signInUser = user;
print(signInUser.email);
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.orange,
title: Text(
"messages",
),
actions: [
IconButton(
onPressed: () {
_auth.signOut();
Navigator.pop(context);
},
icon: Icon(Icons.close)),
],
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
padding: EdgeInsets.only(left: 5),
child: Row(
children: [
Expanded(
child: TextField(
onChanged: (value) {
messageText = value;
},
decoration: InputDecoration(
hintText: "Write your message here...",
hintStyle: TextStyle(fontSize: 22),
),
),
),
TextButton(
onPressed: () {
_fireStore.collection("message").add({
"name": messageText,
"email": signInUser.email,
});
},
child: Text(
"Send",
style: TextStyle(fontSize: 20),
),
),
],
),
),
],
),
),
);
}
}
this is image for my collection that i made it to get the messages
and this is the rules in Cloud Firestore Rules section
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /messages/{document} {
allow read, write: if true;
}
}
}