Flutter 使用FirebaseAuth和flift向Firebase注册用户时出现问题
main.dart文件代码Flutter 使用FirebaseAuth和flift向Firebase注册用户时出现问题,flutter,firebase-authentication,Flutter,Firebase Authentication,main.dart文件代码 import 'package:flutter/material.dart'; import 'package:flash_chat/screens/login_screen.dart'; import 'screens/chat_screen.dart'; import 'screens/login_screen.dart'; import 'screens/registration_screen.dart'; import 'screens/welcome_scr
import 'package:flutter/material.dart';
import 'package:flash_chat/screens/login_screen.dart';
import 'screens/chat_screen.dart';
import 'screens/login_screen.dart';
import 'screens/registration_screen.dart';
import 'screens/welcome_screen.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(FlashChat());
}
class FlashChat extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: WelcomeScreen.id,
routes: {
WelcomeScreen.id: (context) => WelcomeScreen(),
LoginScreen.id: (context) => LoginScreen(),
ChatScreen.id: (context) => ChatScreen(),
RegistrationScreen.id: (context) => RegistrationScreen(),
},
);
}
}
注册提供电子邮件和密码的用户-
import 'package:flash_chat/constants.dart';
import 'package:flash_chat/screens/chat_screen.dart';
import 'package:flutter/material.dart';
import 'package:flash_chat/components/rounded_botton.dart';
import 'package:firebase_auth/firebase_auth.dart';
class RegistrationScreen extends StatefulWidget {
static const String id = 'registration_screen';
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _authe = FirebaseAuth.instance;
String email;
String password;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Hero(
tag: 'logo',
child: Container(
height: 200.0,
child: Image.asset('images/logo.png'),
),
),
SizedBox(
height: 48.0,
),
TextField(
textAlign: TextAlign.center,
onChanged: (value) {
//Do something with the user input.
email = value;
},
decoration:
kTextFieldDecoration.copyWith(hintText: 'Enter your email'),
),
SizedBox(
height: 8.0,
),
TextField(
keyboardType: TextInputType.emailAddress,
obscureText: true,
textAlign: TextAlign.center,
onChanged: (value) {
//Do something with the user input.
password = value;
},
decoration: kTextFieldDecoration.copyWith(
hintText: 'Enter your password'),
),
SizedBox(
height: 24.0,
),
RoundedButton(
color: Colors.blueAccent,
title: 'Register',
onPressed: () async {
try {
// print(email);
// print(password);
final newUser = await _authe.createUserWithEmailAndPassword(
email: email, password: password);
if (newUser != null) {
Navigator.pushNamed(context, ChatScreen.id);
}
} catch (e) {
print(e);
}
},
),
],
),
),
),
);
}
}
添加了firebase-core、auth和firestore的依赖项。
在firebase中创建了具有正确包名的项目。因此,是否仍有解决此问题的方法?
import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
class ChatScreen extends StatefulWidget {
static const String id = 'chat_screen';
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final _authe = FirebaseAuth.instance;
FirebaseUser loggedInUser;
@override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
try {
final user = await _authe.currentUser;
if (user != null) {
loggedInUser = user;
print(loggedInUser.email);
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: null,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
//Implement logout functionality
}),
],
title: Text('⚡️Chat'),
backgroundColor: Colors.lightBlueAccent,
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
onChanged: (value) {
//Do something with the user input.
},
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
onPressed: () {
//Implement send functionality.
},
child: Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
);
}
}
Running Gradle task 'assembleDebug'...
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Waiting for AOSP on IA Emulator to report its views...
Debug service listening on ws://127.0.0.1:54953/D-MEEGyugCI=/ws
Syncing files to device AOSP on IA Emulator...
D/EGL_emulation( 6022): eglMakeCurrent: 0xca3d5420: ver 2 0 (tinfo 0xdcc99f70)
D/eglCodecCommon( 6022): setVertexArrayObject: set vao to 0 (0) 1 0
I/flutter ( 6022): MaterialColor(primary value: Color(0xff2196f3))
I/flutter ( 6022): MaterialColor(primary value: Color(0xff2196f3))
I/flutter ( 6022): Color(0xff378ddf)
I/flutter ( 6022): Color(0xff876d97)
I/flutter ( 6022): Color(0xffa1637f)
I/flutter ( 6022): Color(0xffbb5969)
I/flutter ( 6022): Color(0xffc25662)
I/flutter ( 6022): Color(0xffc9535b)
I/flutter ( 6022): Color(0xffd15055)
I/flutter ( 6022): Color(0xffd84d4e)
I/flutter ( 6022): Color(0xffdf4a48)
I/flutter ( 6022): Color(0xffe74841)
I/flutter ( 6022): Color(0xffee453b)
I/flutter ( 6022): MaterialColor(primary value: Color(0xfff44336))
W/IInputConnectionWrapper( 6022): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper( 6022): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 6022): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 6022): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 6022): endBatchEdit on inactive InputConnection
I/2709.flash_cha( 6022): Background concurrent copying GC freed 40728(5MB) AllocSpace objects, 36(1156KB) LOS objects, 49% free, 1815KB/3MB, paused 8.548ms total 39.681ms
W/System ( 6022): Ignoring header X-Firebase-Locale because its value was null.