1

I'm working on a Flutter project. Here is my main.dart file.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}

While running it, here is the error I obtained :

ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)

What can I try to fix this?

6
  • can you share more code? Commented May 9, 2024 at 12:27
  • This is the part causing the issue in my code. Commented May 9, 2024 at 12:32
  • I noticed you've made some edits to your question after I made some changes for clarity. Just a heads-up, the edits I made were aimed at improving understanding and assisting you more effectively. Commented May 9, 2024 at 13:13
  • Yes, the modifications you suggested have been applied to my question to improve its clarity and understanding, as well as to assist me more effectively. Commented May 9, 2024 at 13:21
  • Did you manage to solve the issue? I am getting the same error Commented May 28, 2024 at 7:45

1 Answer 1

0

Try this option instead of DefaultFirebaseOptions.currentPlatform:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: const FirebaseOptions(
      apiKey: "project apikey",
      appId: "project appId",
      messagingSenderId: "project messagingSenderId",
      projectId: "projectId",
    ),
  );
  runApp(const MyApp());
}

You can find your apiKey, appId, messagingSenderId and ProjectId from firebase's setting Project settings/General.

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

2 Comments

Yes, I've tried that as well, but I still get the same error. I can't seem to understand.
Encountered same issue.

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.