0

I have flutter application running on real android device. I am using auth, firestore, storage, cloud functions. I am trying to use firebase emulators but with no luck. Here are things I tried:

In my main function in Flutter I am calling this function:

Future<void> useEmulators() async {
  // this is the ip of my mac on the router
  final ip = "192.168.1.133";
  await FirebaseAuth.instance.useAuthEmulator(ip, 9099);
  await FirebaseStorage.instance.useStorageEmulator(ip, 9199);
  FirebaseFirestore.instance.useFirestoreEmulator(ip, 8080, sslEnabled: false);
  FirebaseFunctions.instanceFor(region: 'us-east4')
       .useFunctionsEmulator(ip, 5001);
}

I am calling this function from my main() like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  // this is where the function is called
  await useEmulators();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  final sharedPreferences = await SharedPreferences.getInstance();
  await preloadSVGs();
  final hiveDbService = HivedbService();
  await hiveDbService.init();
  return runApp(
    ProviderScope(
      // observers: [Logger()],
      overrides: [
        sharedPreferencesProvider.overrideWithValue(sharedPreferences),
        hiveDbServiceProvider.overrideWithValue(hiveDbService),
      ],
      child: App(),
    ),
  );
}

Then I updated firebase.json like this:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"],
    "source": "functions",
    "runtime": "nodejs12"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "host": "192.168.1.133",
      "port": 9099
    },
    "functions": {
      "host": "192.168.1.133",
      "port": 5001
    },
    "firestore": {
      "host": "192.168.1.133",
      "port": 8080
    },
    "ui": {
      "enabled": true,
      "port": 4000
    },
    "pubsub": {
      "host": "192.168.1.133",
      "port": 8085
    },
    "storage": {
      "host": "192.168.1.133",
      "port": 9199
    }
  }
}

I updated AndroidManifest.xml like this:

    <application android:usesCleartextTraffic="true">

    </application>

I ran the emulators like this:

firebase emulators:start

When I try to sign up, nothing happens.
I expect that a new auth record or document to be created but nothing happens. The app keep waiting for a while and it times out.

Could someone please tell me what I am doing wrong??

Thanks in advance.

0

1 Answer 1

2

I finally managed to access firebase emulator from my android device. What a relief. I had two problems.

I fixed the ip addresses inside my firestore.json like this:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"],
    "source": "functions",
    "runtime": "nodejs12"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "host": "0.0.0.0",
      "port": 9099
    },
    "functions": {
      "host": "0.0.0.0",
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0",
      "port": 8080
    },
    "ui": {
      "enabled": true,
      "port": 4000
    },
    "pubsub": {
      "host": "0.0.0.0",
      "port": 8085
    },
    "storage": {
      "host": "0.0.0.0",
      "port": 9199
    }
  },
  "hosting": [
    {
      "target": "public",
      "public": "web",
      "ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
    },
    {
      "target": "admin",
      "public": "admin",
      "ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
    }
  ]
}

The other problem I had was that I was not using wifi. You must have wifi and both your mobile and emulators should be on the same router.

Finally I am able to test my code on a real device with local firebase.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.