FieldValue.increment for Cloud Firestore in Flutter - firebase

I'm creating a android app using flutter and Firebase Firestore.
I would like to add that new FieldValue.increment functionality which is apperently available in Firebase since this april but i've got an error:
The method 'increment' isn't defined for the class 'FieldValue'.
Here is my code that i've tried:
onTap: (){
Firestore.instance.document('docRef').updateData({"numberOfDocs": FieldValue.increment(1)});
}
I'm just a beginner programmer but when i checked that field_value.dart file it's missing "increment" implementation.
So, is it right answer to say that flutter team didn't yet implemented that functionality?
I've seen a tutorial where increment is already used in some .js code but for me it's not working.
https://www.youtube.com/watch?v=8Ejn1FLRRaw

This has been added to the cloud_firestore Flutter plugin as of version 0.10.0.
You can use FieldValue.increment with a double (e.g. FieldValue.increment(1.0)) or an int (e.g. FieldValue.increment(2)).
Here is the relevant pull request.
If anyone reads this before the version is pushed to Pub: The link to the version will not yet work, but in the meantime you can use this Git commit, which contains a fix I did.

User that Import:
import 'package:cloud_firestore/cloud_firestore.dart';
For Increment:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(1)});
For Decrement:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(-1)});

As far as I can see in the change log for FlutterFire, the increment operator has not been added yet.
I added a feature request to the repo.

Related

OneSignal.isPushNotificationsEnabled doesn't return promised value

After initialization OneSignal SDK my code trying to fetch user information with OneSignal.isPushNotificationsEnabled().
Sometimes it worked, but sometimes it doesn't return anything (pending).
The initialization code is fully copied from the official documentation.
Seems the OneSignal API doesn't answer that request. How it can be solved?
Solved. The OneSignal variable initialized before SDK import.

Why do I get "Client is not yet ready to issue requests" - Firestore get() fails when called by Schedule function

In my Firebase project I have a functions.pubsub.schedule().onRun() that runs every 5 minutes to perform some calendar related tasks. It needs to look up in my Firestore collections and does so with a .get() query.
This has been working fine until sometime 2020-03-13 in the morning where the function started to throw
2020-03-13 09:36:02.326 CET scheduledHooks 1042277797598294
Error: INTERNAL ERROR: Client is not yet ready to issue requests.
at Firestore.get projectId [as projectId] (/srv/functions/node_modules/#google-cloud/firestore/build/src/index.js:401:19)
at Query.toProto (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1556:42) at Query._get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1466:30)
at Query.get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1457:21)
at FirebaseActivitiesCollection.<anonymous> (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:32:40)
at Generator.next (<anonymous>) at /srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:8:71
at new Promise (<anonymous>) at __awaiter (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:4:12)
at FirebaseActivitiesCollection.getActivitiesByInterval (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:27:16)
I can't track that I have changed anything, could it be that Firebase made some changes that I should be aware of or am I missing an obvious clue in this error message?
My other Cloud Functions and the Firebase JavaScript SDK still work fine.
Extra info: The same code works on my other environment where I have not deployed. This of course led me to search for changes in the deployed code, but I can't find any!?
It seems by the error, that the project was not connected and initialized correctly. As per the official API here, this error occurs when:
Returns the Project ID for this Firestore instance. Validates that
initializeIfNeeded() was called before.
I would recommend you to check that. In case you are still facing, I would say for you to contact the Firebase Free Support. Since you mentioned that it was working until yesterday morning, it might be some change on their side.
Hope this helps!
This turned out to be a very simple matter of initialization of Firebase:
Before I had:
// Initialize firebase
admin.initializeApp(functions.config().firebase)
..And it worked fine until it didn't anymore.
Now I have:
// Initialize firebase
admin.initializeApp()
... And it works again
Got the same error while running some jest tests involving firebase. Problem was that I didn't have GOOGLE_APPLICATION_CREDENTIALS environment variable set.
Since I was running them locally on my machine, exporting this variable in terminal before running the tests was sufficient for my case:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials/file.json

Error : FacebookSDK has not been initialized yet using react native

So I am trying to add a facebook sign up connect to firebase to my react-native application and expo keep telling me this error . I dont really know what to do because I thought that Facebook SDK was auto initialized on Application start.
This is my Sign In function.
According to the documentation: https://docs.expo.io/versions/latest/sdk/facebook/, you need to initialize the SDK with this function:
Facebook.initializeAsync(appId: string | undefined, appName: string | undefined): Promise<void>
Calling this method ensures that the SDK is initialized. You have to call this method before calling logInWithReadPermissionsAsync to ensure that Facebook support is initialized properly.
You should call this method in your App.js or any top level component.
Came here from Github, after upgrading from Expo SDK 35 => 36 ran into this issue.
Adding this line to app.json fixed it:
"facebookAutoInitEnabled": true
From docs: https://docs.expo.io/versions/latest/sdk/facebook/
The above answer may work as well, but this fixed it in my case

How to crash Flutter App for Android and iOS (to test Firebase Crashlytics)?

I want to test out crash report using Firebase Crashlytics in my Flutter App. I need to Fatal crash my flutter app for both Android and iOS programmatically. Any idea?
If you have the flutter_crashlytics plugin, you can test it using
FirebaseCrashlytics.instance.crash();
You should be able to throw a Dart exception by doing this anywhere in your Flutter app:
throw Exception("This is a crash!");
Or by using an arbitrary object:
throw "This is a crash!";
You can find more info about dart exceptions in the language tour and if you want, you can create your own custom Exception type as explained in this SO answer
integrate firebase_crashlytics
Crashlytics.instance.crash();
using firebase_crashlytics plugin
FirebaseCrashlytics.instance.crash();
You should enable crashlytics
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
if (kDebugMode) {
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);//disable false
}else{
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
}
You can crash you Flutter app by many ways:
throw(Exception('Hello Crashlytics')); like in #Xavi Rigau's answer
List list;
list.add("add");
Like #Paresh Mangukiya does.
3.
final a = [12];
print(a[1]);
But there is 2 IMPORTANT thing you should consider:
You should run your app in release mode, for example with the command flutter run --release
In Crashlytycs console, by default shown only fatal crashes. The crashes above are non-fatal crashes. So, you should clear the filter to see all crash types. (Filter can be filtered in crashlytics dashboard by clicking (x) button near filter). More on this you can read from this SO answer
Check this out . This is from the official documentation. https://firebase.flutter.dev/docs/crashlytics/usage
Once your configuration is proper according to documentation for crashlytics, FirebaseCrashlytics.instance.crash(); will crash the app.

Stream removed error in Firebase Functions

I have a function that runs on the creation of a new user. The function typically fails on the first call after not being called for a while. I'm hoping someone could help me figure out what could be causing this.
export const onUserCreate = functions.auth.user().onCreate(event => {
const user: User = {
userId: event.data.uid,
email: event.data.email
};
return db.doc(`users/${user.userId}`).set(data);
});
As I use the same like below and it works well:
exports.onUserCreate = functions.auth.user().onCreate(event => {
const user = event.data
console.log(user);
})
This is hardly a permanent answer as this is something Google seems to be looking into.
In the meantime though I did some research on similar error messages across the Google libraries and it looks like it happens occasionally when you initialise a Google library outside of a function (ie. at the top of your file).
I've added this line within the function being executed instead of within the global scope and it looks to have stopped the error:
const datastore = require('#google-cloud/datastore')({});
For example instead of:
const datastore = require('#google-cloud/datastore')({});
module.exports.myFunction = function() {
datastore.get(key).then()....
}
you do
module.exports.myFunction = function() {
const datastore = require('#google-cloud/datastore')({});
datastore.get(key).then()....
}
That would go for Firebase libraries too – not just datastore. This isn't best-practice and should be changed back when the bug is fixed.
This seems to be an error with Firebase Functions right now and is being looked into.
EDIT:
Hello all, Sebastian from the Firestore SDK team here. We believe this
issue is related to the recent update of the GRPC Client SDK and have
been running tests with GRPC 1.7.1. So far, we have not been able to
reproduce this issue with this older GRPC version.
#google-cloud/firestore is now at 0.10.1. If you update your
dependencies, you will be able to pull in this release.
Thanks for your patience.
Sebastian
I had the same issue. Turns out this is a bug in the Firestore Node module. Update #google-cloud/firestore to version 0.10.1 and it should fix the issue.

Resources