Firebase Database connection was forcefully killed by the server: Different region #flutter

Viewed 3733

I am building the app with flutter and integrating the firebase. In the start, my app works completely fine but after some time a faced this problem firebase forcefully kill the connection between the app and the server.

W/PersistentConnection( 4812): pc_0 - Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Database lives in a different region. Please change your database URL to https://multiapp-f0e1e-default-rtdb.europe-west1.firebasedatabase.app

6 Answers

This happens because, when you created your project at that time the Realtime Database was not enabled, that's why the google-services.json file don't contain the url to our Realtime Database.

I was also faced with a same problem, here's what I did.

We can also download an updated google-services.json file. But I manually added the url.

from this:

{
  "project_info": {
    "project_number": "xxx4071207xxx",
    "project_id": "xxxantonixxx",
    "storage_bucket": "xxxantonixxx.appspot.com"
  },

to this:

{
  "project_info": {
    "project_number": "xxx4071207xxx",
    "firebase_url": " https://multiapp-f0e1e-default-rtdb.europe-west1.firebasedatabase.app",
    "project_id": "xxxantonixxx",
    "storage_bucket": "xxxantonixxx.appspot.com"
  },

You'll get your database Url from here:

enter image description here

But wait... it still might not work.

If you make changes to the google-services.json file. And recompile the app from start, still the changes DONOT take place immediately.

In my case, even if I deleted the google-services.json file and recompile the app again, it was working similar to before deleting.

Solution:##

So, in that case, we'll have to delete the build and .dart_tool folder in our project folder,

Or run flutter clean in terminal

and then recompile the app again from start.

I came to know about this after I wasted my 2 whole days, working in some other direction. That's why I had to share it, so that it doesn't happen to anybody else.

When you create a real-time database for the first time, DATABASE_URL is added to your config file (GoogleService-Info.plist & google-services.json) file on the server-side. You may be getting an error because the (GoogleService-Info.plist & google-services.json) is outdated and does not contain DATABASE_URL. You can download your updated (GoogleService-Info.plist & google-services.json) file from your Project Settings and try again.

Even after @Madhav's(accepted answer) solution didn't work, try creating a fresh project and follow the steps mentioned in accepted answer.

Do like the first comment said after that just run the command flutter clean and build again and everything should work just fine!

make sure to delete build and .dart_tool folders, then it works

This is ALSO the error message if you are silly like me and are trying to connect to Google Girebase Database instead of what I actually had set up which is Google Firestore.

// Bad:
import 'package:firebase_database/firebase_database.dart';
DatabaseReference ref = FirebaseDatabase.instance.ref("users/123");


// Good
var db = FirebaseFirestore.instance;
import 'package:cloud_firestore/cloud_firestore.dart';

// (or vice versa)
Related