firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.67k stars 3.96k forks source link

Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null) #9456

Closed absolutelydigital closed 2 years ago

absolutelydigital commented 2 years ago

Firebase won't initialize

`E/flutter ( 9796): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to
establish connection on channel., null, null)
[        ] E/flutter ( 9796): #0      FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:199:7)
[        ] E/flutter ( 9796): <asynchronous suspension>
[        ] E/flutter ( 9796): #1      MethodChannelFirebase._initializeCore
(package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
[        ] E/flutter ( 9796): <asynchronous suspension>
[        ] E/flutter ( 9796): #2      MethodChannelFirebase.initializeApp
(package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
[        ] E/flutter ( 9796): <asynchronous suspension>
[        ] E/flutter ( 9796): #3      Firebase.initializeApp (package:firebase_core/src/firebase.dart:40:31)
[        ] E/flutter ( 9796): <asynchronous suspension>
[        ] E/flutter ( 9796): #4      main (package:stepchain/main.dart:156:3)
[        ] E/flutter ( 9796): <asynchronous suspension>
[        ] E/flutter ( 9796): 
`

This is my main.dart where I'm calling firebase

`Future<void> main() async {
  // final int helloAlarmID = 0;

  SharedPreferences.setMockInitialValues({});
  if (Platform.isAndroid) SharedPreferencesAndroid.registerWith();
  if (Platform.isIOS) SharedPreferencesIOS.registerWith();

  DartPluginRegistrant.ensureInitialized();
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
etc
etc
etc
runApp(MyApp());
`

Output of flutter doctor -v

`[✓] Flutter (Channel stable, 3.3.0, on macOS 12.4 21F79 darwin-x64, locale en)
    • Flutter version 3.3.0 on channel stable at /Applications/MAMP/htdocs/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (3 days ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/charbelhage/Library/Android/sdk
    • Platform android-33, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13F100
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] VS Code (version 1.41.1)
    • VS Code at /Users/charbelhage/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • macOS (desktop)                    • macos         • darwin-x64     • macOS 12.4 21F79 darwin-x64
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 104.0.5112.101

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!`

Tried everything, read every issue and tried every solution on the web, nothing seems to work.

darshankawar commented 2 years ago

`Future main() async { // final int helloAlarmID = 0;

SharedPreferences.setMockInitialValues({}); if (Platform.isAndroid) SharedPreferencesAndroid.registerWith(); if (Platform.isIOS) SharedPreferencesIOS.registerWith();

DartPluginRegistrant.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); etc etc etc runApp(MyApp()); `

@absolutelydigital WidgetsFlutterBinding.ensureInitialized(); should be the first line inside main() method followed by Firebase.initializeApp(). Also, not sure why are you using DartPluginRegistrant.ensureInitialized();.

So updated main() method should be:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
 // SharedPreferences related code here
 // runApp(MyApp());

Try above and see if it helps or not.

If not, provide flutter doctor -v, pubspec.yaml in properly formatted text.

absolutelydigital commented 2 years ago

hello @darshankawar , this didn't work. it is worth noting that the project was built way before null-safety and still wasn't upgraded.

please find below the pubspec.yaml

name: stepchain
description: A responsible fitness app

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 2.0.0+1

environment:
  sdk: '>=2.7.0 <3.0.0'

dependency_overrides:

dependencies:
  flutter:
    sdk: flutter
  easy_localization: ^3.0.0
  cupertino_icons:
  fluttertoast:
  percent_indicator:
  page_indicator:
  flutter_animation_progress_bar:
  font_awesome_flutter:
  pagination:
  step_progress_indicator:
  auto_size_text:
  #flutter_svg:
  flutter_keyboard_visibility:
  story_view: ^0.13.1
  flutter_custom_clippers:
  flutter_swiper:
  simple_animations: ^5.0.0+2
  recase:
  shimmer:
  fl_chart: ^0.55.0
  flutter_spinkit:
  simple_tooltip:
  camera:
  vibration:
  location:
  #pedometer:
  video_player:
  shared_preferences:
  shared_preferences_android:
  shared_preferences_ios:
  device_info:
  device_apps:
  path_provider:
  contacts_service:
  permission_handler:
  store_redirect:
  share:
  connectivity:
  wakelock:
  image_picker:
  photo_manager:
  cached_network_image:
  http:
  flutter_widget_from_html_core:
  flutter_downloader:
  android_path_provider: ^0.3.0
  url_launcher:
  intl:
  provider:
  date_format:
  enum_to_string:
  after_layout:
  #get_it:
  # health_kit: ^0.0.4
  package_info_plus:
  webview_flutter:
  photo_view:
  gallery_saver:
  flutter_linkify:
  carousel_slider:
  flutter_html: ^2.2.1
  syncfusion_flutter_pdfviewer:
  native_device_orientation:
  youtube_player_flutter:
  youtube_parser:
  battery:
  #flutter_webview_plugin:
  flutter_toastr:
  flutter_file_manager:
  searchfield: ^0.7.0
  in_app_purchase:
  health: ^4.1.1
  get_it: ^7.2.0
  pedometer: ^3.0.0
  flutter_login_facebook:

  firebase_core: ^1.21.1
  firebase_auth: ^3.7.0
  firebase_analytics: ^9.3.3
  firebase_crashlytics: ^2.8.9
  firebase_performance: ^0.8.2+4
  firebase_remote_config: ^2.0.16
  firebase_messaging: ^13.0.0
  firebase_in_app_messaging: ^0.6.0+23
  firebase_dynamic_links: ^4.3.6
  firebase_database: ^9.1.3
  cloud_functions: ^3.3.6
  cloud_firestore: ^3.4.6
  firebase_core_platform_interface: ^4.4.1
  firebase_storage: ^10.3.7
  flutter_local_notifications: ^9.1.4
  flutter_svg: ^0.23.0+1
  google_maps_flutter: ^2.2.0
  flutter_facebook_auth: ^4.4.1
  google_sign_in: ^5.4.1
  sign_in_with_apple: ^4.1.0
  #image_cropper: ^2.0.3

dev_dependencies:
  flutter_test:
    sdk: flutter
#  flutter_multiple_image_picker:

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  #  assets:
  #  - assets/launch_settings/
  #  - assets/launch_settings_gs/

  fonts:
    - family: Nexa
      fonts:
        - asset: fonts/NexaBold.otf
          weight: 700
        - asset: fonts/NexaXBold.otf
          weight: 900
        - asset: fonts/NexaRegular.otf
          weight: 400 #style: normal
        - asset: fonts/NexaThin.otf
          weight: 100 #style: italic
        - asset: fonts/NexaHeavy.otf
          weight: 500
    - family: CIconBottom
      fonts:
        - asset: fonts/CIconBottom.ttf
          weight: 400
    - family: CIconTop
      fonts:
        - asset: fonts/CIconTop.ttf
          weight: 400
    - family: GE_SS_Two
      fonts:
        - asset: fonts/GE_SS_Two_Medium.otf
          weight: 700
        - asset: fonts/GE_SS_Two_Bold.otf
          weight: 900
        - asset: fonts/GE_SS_Two_Light.otf
          weight: 400
    - family: PublicaSans
      fonts:
        - asset: fonts/PublicaSans-Bold.otf
          weight: 700
        - asset: fonts/PublicaSans-Bold.otf
          weight: 900
        - asset: fonts/PublicaSans-Light.otf
          weight: 400 #style: normal
        - asset: fonts/PublicaSans-Thin.otf
          weight: 100
        - asset: fonts/PublicaSans-Bold.otf
          weight: 500

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages

Below is the output of flutter doctor -v

[✓] Flutter (Channel stable, 3.3.0, on macOS 12.4 21F79 darwin-x64, locale en)
    • Flutter version 3.3.0 on channel stable at /Applications/MAMP/htdocs/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (4 days ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/charbelhage/Library/Android/sdk
    • Platform android-33, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13F100
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] VS Code (version 1.41.1)
    • VS Code at /Users/charbelhage/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • macOS (desktop)                    • macos         • darwin-x64     • macOS 12.4 21F79 darwin-x64
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 104.0.5112.101

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Tried to run the project with and without --no-sound-null-safety, same result. Debug, Release, Profile everything fails. The application is stuck on splash screen with this error so it is a major block.

darshankawar commented 2 years ago

@absolutelydigital Below are some of the solutions for the same error as you are getting, so try them and see if anyone of them helps:

https://github.com/firebase/flutterfire/issues/9113#issuecomment-1188555746 https://github.com/firebase/flutterfire/issues/9088#issuecomment-1185601099 https://github.com/firebase/flutterfire/issues/9033#issuecomment-1175249144 https://github.com/firebase/flutterfire/issues/9028#issuecomment-1173025928

CalvinAH commented 2 years ago

Hi, i am having similar issue as well. Is there any update on the issue or has it been resolved? because I need help in this regard

this is my flutter doctor -v image

and this is my error

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

0 FirebaseCoreHostApi.initializeCore

package:firebase_core_platform_interface/…/pigeon/messages.pigeon.dart:199

#1 MethodChannelFirebase._initializeCore package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:29 #2 MethodChannelFirebase.initializeApp package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:73 #3 Firebase.initializeApp package:firebase_core/src/firebase.dart:40 My pubspec.yaml `name: location_capacity_tracker description: A new Flutter project. environment: sdk: ">=2.17.6 <3.0.0" dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 firebase_core: ^1.21.1 firebase_auth: ^3.7.0 cloud_firestore: ^3.4.6 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true assets: - assets/images/ `
google-oss-bot commented 2 years ago

Hey @absolutelydigital. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 2 years ago

Since there haven't been any recent updates here, I am going to close this issue.

@absolutelydigital if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

amirVirtuenetz commented 1 year ago

flutter doctor -v

same issue i am facing