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.68k stars 3.97k forks source link

🐛 [firebase_auth] throw PlatformException(code: errorCode, message: errorMessage, details: errorDetails); #5195

Closed SirMajed closed 3 years ago

SirMajed commented 3 years ago

Hello, I have a weird problem, I have an existed user in my firebase auth. so when I register a new user the code in try and catch the code for the user registration:

AuthResult res;
    try {
      res = await _instance.createUserWithEmailAndPassword(
          email: account.getEmail(), password: account.getPassword());
      (account as User).id = res.user.uid;

      await Firestore.instance
          .collection('Users')
          .document((account as User).id)
          .setData((account as User).toJson());

      await signIn(account);
    } catch (e) {
      signOut();
      rethrow;
    }

and in the UI, I also have try and catch for firebase exception class that I already made that contains the error codes for firebase auth and firestore.

try {
  User user = new User(
    email: _email,
    password: _password,
    name: _name);
  await user.register();
  Navigator.of(context).pop();
}
on PlatformException
catch (exception) {
  String msg = FirebaseException
    .generateReadableMessage(exception);
  MyBar.customFlushBar(
    context: context,
    message: msg,
    icon: Icons.warning_amber_rounded);
}
}

But the problem is that the error is not even catched and the system stopped at this line in debug: Screenshot_8

I just want it to display the snackbar error instead of the system stop working, I'm sorry for my English

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 1.22.6, on Microsoft Windows [Version 10.0.19042.804], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [√] Android Studio (version 4.0) [√] VS Code (version 1.53.2) [√] Connected device (1 available) • No issues found! ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.10.5 Flutter SDK 1.22.6 my_notes 1.0.0+1 dependencies: - avatar_glow 1.2.0 [flutter] - cached_network_image 2.5.0 [flutter flutter_cache_manager octo_image] - cloud_firestore 0.13.4 [flutter meta firebase_core cloud_firestore_platform_interface cloud_firestore_web] - cupertino_icons 1.0.0 - firebase_auth 0.16.1 [meta firebase_core firebase_auth_platform_interface firebase_auth_web flutter] - firebase_storage 3.0.3 [flutter firebase_core] - flushbar 1.10.4 [flutter] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_svg 0.19.0 [path_drawing xml vector_math meta flutter] - font_awesome_flutter 8.11.0 [flutter] - image_picker 0.6.7+22 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - intl 0.16.1 [path] - molten_navigationbar_flutter 0.0.1 [flutter] - multiavatar 0.1.2 [flutter crypto] - provider 4.3.1 [flutter nested collection] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - archive 2.0.13 [crypto args path] - args 1.6.0 - async 2.5.0-nullsafety.1 [collection] - boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner] - characters 1.1.0-nullsafety.3 - charcode 1.2.0-nullsafety.1 - clock 1.1.0-nullsafety.1 - cloud_firestore_platform_interface 1.1.2 [flutter meta collection firebase_core plugin_platform_interface] - cloud_firestore_web 0.1.1+2 [flutter flutter_web_plugins firebase http_parser meta firebase_core cloud_firestore_platform_interface js] - collection 1.15.0-nullsafety.3 - convert 2.1.1 [charcode typed_data] - crypto 2.1.5 [collection convert typed_data] - fake_async 1.2.0-nullsafety.1 [clock collection] - ffi 0.1.3 - file 5.2.1 [intl meta path] - firebase 7.3.3 [http http_parser js] - firebase_auth_platform_interface 1.1.8 [flutter meta plugin_platform_interface] - firebase_auth_web 0.1.3+1 [firebase_auth_platform_interface flutter flutter_web_plugins firebase http_parser meta js] - firebase_core 0.4.5 [firebase_core_platform_interface flutter meta firebase_core_web] - firebase_core_platform_interface 1.0.4 [flutter meta plugin_platform_interface quiver] - firebase_core_web 0.1.1+2 [firebase firebase_core_platform_interface flutter flutter_web_plugins meta js] - flutter_blurhash 0.5.0 [flutter meta] - flutter_cache_manager 2.1.1 [flutter path_provider uuid http path sqflite pedantic clock file rxdart image] - flutter_plugin_android_lifecycle 1.0.11 [flutter] - flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math] - http 0.12.2 [http_parser path pedantic] - http_parser 3.1.4 [charcode collection source_span string_scanner typed_data] - image 2.1.19 [archive xml meta] - image_picker_platform_interface 1.1.6 [flutter meta http plugin_platform_interface] - js 0.6.2 - matcher 0.12.10-nullsafety.1 [stack_trace] - meta 1.3.0-nullsafety.3 - nested 0.0.4 [flutter] - octo_image 0.3.0 [flutter flutter_blurhash] - path 1.8.0-nullsafety.1 - path_drawing 0.4.1+1 [vector_math meta path_parsing flutter] - path_parsing 0.1.4 [vector_math meta] - path_provider 1.6.27 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows] - path_provider_linux 0.0.1+2 [path xdg_directories path_provider_platform_interface flutter] - path_provider_macos 0.0.4+8 [flutter] - path_provider_platform_interface 1.0.4 [flutter meta platform plugin_platform_interface] - path_provider_windows 0.0.4+3 [path_provider_platform_interface meta path flutter ffi win32] - pedantic 1.9.2 [meta] - petitparser 3.1.0 [meta] - platform 2.2.1 - plugin_platform_interface 1.0.3 [meta] - process 3.0.13 [file intl meta path platform] - quiver 2.1.5 [matcher meta] - rxdart 0.25.0 - sky_engine 0.0.99 - source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph] - sqflite 1.3.2+3 [flutter sqflite_common path] - sqflite_common 1.0.3+1 [synchronized path meta] - stack_trace 1.10.0-nullsafety.1 [path] - stream_channel 2.1.0-nullsafety.1 [async] - string_scanner 1.1.0-nullsafety.1 [charcode source_span] - synchronized 2.2.0+2 - term_glyph 1.2.0-nullsafety.1 - test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0-nullsafety.3 [collection] - uuid 2.2.2 [crypto convert] - vector_math 2.1.0-nullsafety.3 - win32 1.7.4+1 [ffi] - xdg_directories 0.1.2 [meta path process] - xml 4.5.1 [collection convert meta petitparser] ```

Pubspec.yaml

Click To Expand ``` version: 1.0.0+1 environment: sdk: ">=2.7.0 <3.0.0" dependencies: flutter: sdk: flutter firebase_auth: 0.16.1 cloud_firestore: 0.13.4 provider: 4.3.1 intl: ^0.16.1 flutter_svg: 0.19.0 molten_navigationbar_flutter: ^0.0.1 font_awesome_flutter: ^8.11.0 avatar_glow: ^1.2.0 cached_network_image: ^2.5.0 image_picker: ^0.6.7+22 firebase_storage: 3.0.3 multiavatar: ^0.1.2 flushbar: ^1.10.4 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.0 dev_dependencies: flutter_test: sdk: flutter # For information on the generic Dart part of this file, see the # following page: https://dart.dev/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 assets: - assets/ ```
markusaksli-nc commented 3 years ago

Hi @SirMajed What exactly is the problem here? Could you provide expected vs actual behavior? For example, this works when trying to register an existing email:

  // Example code for registration.
  void _register() async {
    try {
      final User user = (await _auth.createUserWithEmailAndPassword(
        email: _emailController.text,
        password: _passwordController.text,
      ))
          .user;
      if (user != null) {
        setState(() {
          _success = true;
          _userEmail = user.email;
        });
      } else {
        _success = false;
      }
    } catch (e) {
      print(e.runtimeType);
      print("caught $e");
    }
  }
I/flutter ( 7702): FirebaseAuthException
I/flutter ( 7702): caught [firebase_auth/email-already-in-use] The email address is already in use by another account.

Thank you

SirMajed commented 3 years ago

Hi @SirMajed What exactly is the problem here? Could you provide expected vs actual behavior? For example, this works when trying to register an existing email:

  // Example code for registration.
  void _register() async {
    try {
      final User user = (await _auth.createUserWithEmailAndPassword(
        email: _emailController.text,
        password: _passwordController.text,
      ))
          .user;
      if (user != null) {
        setState(() {
          _success = true;
          _userEmail = user.email;
        });
      } else {
        _success = false;
      }
    } catch (e) {
      print(e.runtimeType);
      print("caught $e");
    }
  }
I/flutter ( 7702): FirebaseAuthException
I/flutter ( 7702): caught [firebase_auth/email-already-in-use] The email address is already in use by another account.

Thank you

Thank you for your answer, It not supposed to freeze the IDE, it supposed to gives an error and continue. because when the user trying to delete his account, he must provide his password to reauthenticate, so if I provide a wrong password it should give a snackbar with error but instead the vscode IDE freezes, Check my next comment pleae

SirMajed commented 3 years ago

Here also didnt worked for me when user trying to delete his account, the vscode freezes and gives platform exception

Code for UI

try {
  await Authentication.deleteAccount(
    email: user.getEmail(),
    password: _password,
    userID: user.getID(),
  );
  MyBar.customFlushBar(
    context: context,
    message: 'Account deleted!',
    icon: Icons.check);
  Navigator.pop(context);
}
on PlatformException
catch (ex) {
  String msg = FirebaseException
    .generateReadableMessage(ex);
  MyBar.customFlushBar(
    context: context,
    message: msg,
    icon: Icons.check);
}

Delete account method

static Future deleteAccount(
      {String userID, String password, String email}) async {
    print(email);
    try {
      FirebaseUser user = await _instance.currentUser();
      AuthCredential credentials =
          EmailAuthProvider.getCredential(email: email, password: password);
      print(user);
      AuthResult result = await user.reauthenticateWithCredential(credentials);
      await Database.deleteUserDocuments(userID); // called from database class
      await result.user.delete();
      return true;
    } on FirebaseException catch (e) {
      print(e.toString());
      return Future.error(e);
    }
  }
SirMajed commented 3 years ago

Well suddenly it was from the debugging mode, I was able to solve it by unchecking the 'Uncaught Exceptions' checkbox. it runs fine and the FirebaseException was caught. The problem is coming from VsCode debugger