2

Error Sending the message text to the Firestore Database in Firebase

I have integrated the login functionality and now want to send the typed message to the firestore.

I am new to flutter development and was trying to link the firestore. have tried collection.add() and collection.setData() .

Here is the dart code

import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class ChatScreen extends StatefulWidget {
  @override
  _ChatScreenState createState() => _ChatScreenState();
  static const chat_screen = "chat_screen";
}

class _ChatScreenState extends State<ChatScreen> {
  final _auth = FirebaseAuth.instance;
  final _firestore = Firestore.instance;
  FirebaseUser loggedin;
  String message;

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser();
      loggedin = user;}
catch (e) {print(e);}}
  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: null,
        actions: <Widget>[IconButton(
              icon: Icon(Icons.close),
              onPressed: () {
                //Implement logout functionality
                _auth.signOut();
                Navigator.pop(context);
              }),],
        title: Text('⚡️Chat'),
        backgroundColor: Colors.lightBlueAccent,),
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Container(
              decoration: kMessageContainerDecoration,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[Expanded(child: TextField(
                      onChanged: (value) {
                        //Do something with the user input.
                        message = value;},
                      decoration: kMessageTextFieldDecoration,),),
                  FlatButton(
                    onPressed: () {
                      //Implement send functionality.
                      _firestore
                          .collection('message')
                          .document('recent')
                          .setData({
                        'sender': loggedin.email,
                        'description': message
                      });
                    },
                    child: Text(
                      'Send',
                      style: kSendButtonTextStyle,
[The firebase Collection tree][1]
[Console output on submit][2]


  [1]: https://i.stack.imgur.com/eBAHI.png
  [2]: https://i.stack.imgur.com/cVdGS.png
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Siddharth
  • 41
  • 6
  • Have you checked what the rules for writing to the Firestore in the dashboard? – tomerpacific May 17 '20 at 07:01
  • 1
    Does this answer your question? [firestore: PERMISSION\_DENIED: Missing or insufficient permissions](https://stackoverflow.com/questions/46590155/firestore-permission-denied-missing-or-insufficient-permissions) – tomerpacific May 17 '20 at 07:02
  • Your security rules are blocking access. Without seeing those rules we can't say for sure what you should do or how to resolve the issue. – Doug Stevenson May 17 '20 at 16:34
  • I have fixed it. Changed the rules from false to true and it worked. – Siddharth May 18 '20 at 10:11

1 Answers1

1

After trying several solutions and almost drinking myself to a pulp, I edited a solution I found and it works perfectly. I hope you find it useful.

Firestore rule:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow create: if request.auth != null;
      allow read, write, update, delete: if request.auth != null;
    }
  }
}
Adminixtrator
  • 554
  • 5
  • 8