简体   繁体   中英

Flutter/Firebase: Firebase local emulator not working in Android emulator

I am trying to develop a project using Firebase as a back end. I have used the Firebase Emulator in iOS Simulator, and it works fine. I have added the Android emulator specific settings to my project:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  String host = !kIsWeb && Platform.isAndroid ? '10.0.2.2' : 'localhost';
  await Firebase.initializeApp();
  await FirebaseAuth.instance.useAuthEmulator(host, 9099);
  FirebaseFirestore.instance.settings = Settings(
    host: '$host:8080',
    sslEnabled: false,
    persistenceEnabled: false,
  );

  runApp(const MyApp());
}

I get the following error on auth.

E/flutter ( 7777): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: [firebase_auth/unknown] null
E/flutter ( 7777): #0      MethodChannelFirebaseAuth.createUserWithEmailAndPassword
E/flutter ( 7777): <asynchronous suspension>
E/flutter ( 7777): #1      FirebaseAuth.createUserWithEmailAndPassword
E/flutter ( 7777): <asynchronous suspension>
E/flutter ( 7777): #2      _LoginScreenState.build._submitLogin
E/flutter ( 7777): <asynchronous suspension>
E/flutter ( 7777):

the auth code:

void _submitLogin(String email, String password) async {
      final _auth = FirebaseAuth.instance;
      UserCredential user;
      final firestore = FirebaseFirestore.instance;

      if (_isSignup && _daysOfWeek.values.every((element) => !element)) {
        await showCupertinoDialog(
          context: context,
          builder: (ctx) {
            return CupertinoAlertDialog(
              title: const Text('No Days Selected'),
              content:
                  const Text('Please select days you are available to play'),
              actions: [
                CupertinoDialogAction(
                  onPressed: () {
                    return;
                  },
                  child: const Text('OK'),
                ),
              ],
            );
          },
        );
      }

      if (_isSignup) {
        user = await _auth.createUserWithEmailAndPassword(
            email: email, password: password);

        firestore.collection('users').doc(user.user.uid).set(
          {
            'date_of_birth': _dateOfBirth,
            'name': _name,
            'hip_size': _hipSize,
            'height': _height,
            'tennis_level': _tennisLevel,
            'days_avaialable': _daysOfWeek,
          },
        );
        return;
      }

      await _auth.signInWithEmailAndPassword(email: email, password: password);
    }

I have created the host string just of the android emulator but it still doesn't work. What am I doing wrong?

edit:

The 10.0.2.2 IP space is accessible through the emulators chrome:

10.0.2.2 ip 空间可通过模拟器 chrome 访问:

This was a weird issue. It had to do with how I layed-out my widget tree. The app is using the CupertinoApp model. The issue itself was very complicated and specific.

Issue

The Form child had this tree: Expanded -> ListView -> List<CupertinoFormSection> -> List<Widgets>

Some of the fields are not text inputs, but used modal dialogs. Due to the quirky nature of the ListView, any field that wasn't on screen was cleared of its value when you dismiss the modal dialogues. This was usually the email and password fields. So when the form is saved, the onSaved callback was not working properly for email and password fields. This was the cause of the error.

Solution

The solution was simple; replacing the ListView with a Column and wrapping it in a SingleChildScrollView. So the Form child tree would be...

Expanded -> SingleChildScrollView -> Column -> ...

Now the fields are not cleared and there is no 'null' error. I was validating the form before saving, but I do not know why the validators were not being triggered. But the error has been resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM