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;
}
}
}
I would like the message to go away and the problem resolved.
CodePudding user response:
There are 3 possible points of failure in my opinion and those are with solutions:
- Check whether your firestore database API is enabled for this follow:
- Goto firestore and select your project
- click on Firestore database from the build dropdown situated at the left side
- If you are able to see your database then consider your API is enabled if not you will see the create database button hit create with your required options then it will be automatically enabled.
- If you have enabled the API earlier and are still seeing this error check that your service account has the necessary permissions to access the firestore db this can be verified by:
- Goto cloud console IAM and click on view by roles
- Check whether Firebase Admin SDK Administrator Service Agent has a service account which is something like
firebase-adminsdk-@<project_id>.iam.gserviceaccount.com
and has exact project id as your firebase project id.
- Check on the App Engine Settings page to ensure it's not disabled for your project as it's required to run firestore db.
Also consider adding one document manually to the db then try after some time
If all these didn't work consider contacting firebase support
CodePudding user response:
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 clear
afterwards and then flutter get packages
. I hope I could help you