[Firestore]: Write failed at message/recent: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

Viewed 981

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
1 Answers

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;
    }
  }
}
Related