Search code examples
androidflutterfirebasegoogle-cloud-firestore

Firestore API has not been used in project 416788152002 before or it is disabled


I really don't know what the problem is. But since I want to take data from my Cloud Firestore and want to display it in the app, it doesn't work. I get the error message:

W/Firestore( 8928): (24.4.1) [WatchStream]: (aab9534) Stream closed with status: Status{code=PERMISSION_DENIED, description=Cloud Firestore API has not been used in project 416187152002 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firestore.googleapis.com/overview?project=416187152002 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., cause=null}.

My Body:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:prody/application/auth/todos/observer/index.dart';

class HomeBody extends StatelessWidget {
  const HomeBody({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ObserverBloc, ObserverState>(
      builder: (context, state) {
        if (state is ObserverInitial) {
          return Container();
        } else if (state is ObserverLoading) {
          return Center(
            child: CircularProgressIndicator(
              color: Colors.blueAccent,
            ),
          );
        } else if (state is ObserverFailure) {
          return Center(
            child: Text("Failure"),
          );
        } else if (state is ObserverSuccess) {
          return ListView.builder(
            itemCount: state.todo.length,
            itemBuilder: (context, index) {
            final todo = state.todo[index];
            return Container(
              color: Colors.green,
              height: 50
            );
          });
        }
        return Container(
          child: Text("-"),
        );
      },
    );
  }
}

entities:

import 'package:flutter/material.dart';

import 'package:prody/domain/entities/id.dart';
import 'package:prody/domain/entities/todo_color.dart';

class Todo {
  final UniqueId id;
  final String title;
  final String body;
  final bool done;
  final TodoColor color;

  Todo(
      {required this.id,
      required this.title,
      required this.body,
      required this.done,
      required this.color});

  factory Todo.empty() {
    return Todo(
        id: UniqueId(),
        title: "",
        body: "",
        done: false,
        //color ist 0 also die erste color, bei todo_color.dart
        color: TodoColor(color: TodoColor.predefinedColors[0]));
  }

  Todo copyWith({
    UniqueId? id,
    String? title,
    String? body,
    bool? done,
    TodoColor? color,
  }) {
    return Todo(
      id: id ?? this.id,
      title: title ?? this.title,
      body: body ?? this.body,
      done: done ?? this.done,
      color: color ?? this.color,
    );
  }
}

todo Repository implementation:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:prody/domain/entities/todo.dart';
import 'package:prody/core/Failures/todo_failures.dart';
import 'package:dartz/dartz.dart';
import 'package:prody/domain/repositries/todo_repository.dart';
import 'package:prody/infastructure/extensions/firebase_helpers.dart';
import 'package:prody/infastructure/models/todo_model.dart';

class todoRespositoryImpl implements todoRespository {
  final FirebaseFirestore firestore;
  todoRespositoryImpl({required this.firestore});

  @override
  Stream<Either<TodoFailure, List<Todo>>> watchAll() async* {
    final userDoc = await firestore.userDocument();

    // right side listen on todos
    yield* userDoc.todocollection
        .snapshots()
        .map((snapshot) => right<TodoFailure, List<Todo>>(snapshot.docs
            .map((doc) => TodoModel.fromFirestore(doc).toDomain())
            .toList()))

        // error handling (left side)
        .handleError((e) {
      if (e is FirebaseException) {
        if (e.code.contains("permission-denied") ||
            e.code.contains("PERMISIION_DENIED")) {
          return left(InsufficientPermissions());
        } else {
          return left(UnexpectedFailure());
        }
      } else {
        return left(UnexpectedFailure());
      }
    });
  }

  @override
  Future<Either<TodoFailure, Unit>> create(Todo todo) {
    // TODO: implement create
    throw UnimplementedError();
  }

  @override
  Future<Either<TodoFailure, Unit>> delete(Todo todo) {
    // TODO: implement delete
    throw UnimplementedError();
  }

  @override
  Future<Either<TodoFailure, Unit>> update(Todo todo) {
    // TODO: implement update
    throw UnimplementedError();
  }
}

Rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

The data I want to query

I would like the message to go away and the problem resolved.


Solution

  • My mistake was that I used an outdated google-services.json file. This bug has been keeping me busy 10 hours a day for two days now without finding a solution. I tried all of them, but nothing worked. If you re-paste google-services.json it probably still won't work because you have to run flutter clean afterwards and then flutter pub upgrade. I hope I could help you 🤙