Preamble
This has been a struggle for a good while now and nothing seems to be working, I have seen answers to similar questions here and here but the former was not in flutter and the latter was posted before the recent changes to firebase_auth and I guess most importantly the proposed solutions have not worked.
The Issue
The issue concerns a very very simple initial setup of firebase authentication within flutter. I have set up my firebase project exactly as it suggests here and used this and this to set up firebase_auth.
The code that is causing the problem is as follows:
void _registerTestUser() async {
try {
UserCredential userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!");
print(userCredential.user.email);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
}
When the code is run the error
I/BiChannelGoogleApi(19546): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzao@fb1556c
Is returned.
This answer seems to suggest that the error may well be irrelevant, i.e. the code may work anyway. I put a breakpoint on the line UserCredential userCredential = await FirebaseAuth.instance and stepped over, it continues to .createUserWithEmailAndPassword(, then once stepped over again goes back to the first line and the error is displayed. Stepping over any further breaks out of the function entirely, so the print(userCredential.user.email); line is never executed, which would suggest the code isn't working. Additionally the print(e); line in the catch is not what is outputting this error.
Other Info
Pubspec.yaml
dependencies:
flutter:
sdk: flutter
firebase_core: ^0.5.3 #firebase core flutter sdk
firebase_auth: ^0.18.4+1 #firebase authorisation
android/build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.4' // Google Services plugin
}
android/app/build.gradle
apply plugin: 'com.google.gms.google-services' // Google Services plugin
main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
FirebaseAuth auth = FirebaseAuth.instance;
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FutureBuilder(
// Initialize FlutterFire:
future: _initialization,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return Error();
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return MyHomePage();
}
// Otherwise, show something whilst waiting for initialization to complete
return Loading();
},
),
);
}
}
class Loading extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Firebase is loading'),
),
);
}
}
class Error extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('There has been an error'),
),
);
}
}
class MyHomePage extends StatelessWidget {
void _registerTestUser() async {
try {
UserCredential userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!");
print(userCredential.user.email);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('FlutterFire Test')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text('Register a test user'),
onPressed: () => _registerTestUser(),
color: Colors.blue,
),
],
),
),
);
}
}
Also this error has appeared a few times, although this answer seems to imply it's got nothing to do with my issue and isn't indicative of a problem.
W/ConnectionTracker(19240): java.lang.IllegalArgumentException: Service not registered: lp@fb1556c W/ConnectionTracker(19240): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1729) W/ConnectionTracker(19240): at android.app.ContextImpl.unbindService(ContextImpl.java:1874) W/ConnectionTracker(19240): at android.content.ContextWrapper.unbindService(ContextWrapper.java:792) W/ConnectionTracker(19240): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):1) W/ConnectionTracker(19240): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):2) W/ConnectionTracker(19240): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):10) W/ConnectionTracker(19240): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):2) W/ConnectionTracker(19240): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):3) W/ConnectionTracker(19240): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) W/ConnectionTracker(19240): at java.util.concurrent.FutureTask.run(FutureTask.java:266) W/ConnectionTracker(19240): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):6)
EDIT
Changing the line onPressed: () => _registerTestUser(), in main.dart to onPressed: () {_registerTestUser();} gives the same result but changes the error slightly, the zzao@fb1556c becomes zzao@ebc85e9.