Anonymous
Мое приложение Flutter, использующее Firebase, зависает на экране загрузки после нажатия кнопки [закрыто]
Сообщение
Anonymous » 15 дек 2024, 23:03
Мое приложение Flutter, использующее Firebase, зависает на экране загрузки после нажатия кнопки.
Код кнопки:
Код: Выделить всё
TextButton(onPressed: (){
_submit(context);
},
child: Text("Sign In")
),
Код: Выделить всё
void _submit(BuildContext context){
FocusScope.of(context).unfocus();
if(!_formKey.currentState!.validate()){
//Invalid
return;
}
_formKey.currentState!.save();
context.read().signUp(email: _email, username: _username, password: _password);
}
Настроил параметры подключения к Firebase по инструкции.
Есть такое предупреждение:
Код: Выделить всё
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
**Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.**
√ Built build\app\outputs\flutter-apk\app-debug.apk
для быстрого воспроизведения:
sign_up_screen.dart
Код: Выделить всё
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:social_network/bloc/auth_cubit.dart';
import 'package:social_network/screens/posts_screen.dart';
import 'package:social_network/screens/sign_in_screen.dart';
class SignUpScreen extends StatefulWidget {
static const String id = "sign up screen";
const SignUpScreen({super.key});
@override
State createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State {
final _formKey = GlobalKey();
String _email = "";
String _username = "";
String _password = "";
late final FocusNode _passwordFocusNode;
late final FocusNode _usernameFocusNode;
@override
void initState() {
_passwordFocusNode = FocusNode();
_usernameFocusNode = FocusNode();
super.initState();
}
@override
void dispose() {
_passwordFocusNode.dispose();
_usernameFocusNode.dispose();
super.dispose();
}
void _submit(BuildContext context){
FocusScope.of(context).unfocus();
if(!_formKey.currentState!.validate()){
//Invalid
return;
}
_formKey.currentState!.save();
context.read().signUp(email: _email, username: _username, password: _password);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocConsumer(
listener: (prevState, currState){
if(currState is AuthSignedUp){
Navigator.of(context).pushReplacementNamed(PostsScreen.id);
}
if(currState is AuthFailure){
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: Duration(seconds: 2),
content: Text(currState.message)));
}
},
builder: (context, state){
if(state is AuthLoading){
return Center(child: CircularProgressIndicator());
}
return SafeArea(
child: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 18.0),
child: Text("Social Media App", style: Theme.of(context).textTheme.headlineLarge,),
),
SizedBox(height: 15,),
// email
TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
labelText: "Enter you emaail",
),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_){
FocusScope.of(context).requestFocus(_usernameFocusNode);
},
onSaved: (value){
_email = value!.trim();
},
validator: (value){
if(value!.isEmpty){
return "Please enter your email";
}
return null;
},
),
// username
SizedBox(height: 15,),
TextFormField(
focusNode: _usernameFocusNode,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
labelText: "Enter you username",
),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_){
FocusScope.of(context).requestFocus(_passwordFocusNode);
},
onSaved: (value){
_username = value!.trim();
},
validator: (value){
if(value!.isEmpty){
return "Please enter your username";
}
return null;
},
),
// password
SizedBox(height: 15,),
TextFormField(
focusNode: _passwordFocusNode,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
labelText: "Enter you password",
),
obscureText: true,
textInputAction: TextInputAction.done,
onFieldSubmitted: (_){
_submit(context);
},
onSaved: (value){
_email = value!.trim();
},
validator: (value){
if(value!.isEmpty){
return "Please enter your password";
}
if(value.length < 5){
return "Please enter your password more than 5 characters";
}
return null;
},
),
SizedBox(height: 15,),
TextButton(onPressed: (){
_submit(context);
},
child: Text("Sign Up")
),
TextButton(onPressed: (){
Navigator.of(context).pushReplacementNamed(SignInScreen.id);
},
child: Text("Log In")
//Sign In instead
)
],
),
),
),
)
);
}
),
);
}
}
main.dart
Код: Выделить всё
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:social_network/bloc/auth_cubit.dart';
import 'package:social_network/screens/posts_screen.dart';
import 'package:social_network/screens/sign_in_screen.dart';
import 'package:social_network/screens/sign_up_screen.dart';
import 'package:firebase_core/firebase_core.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => AuthCubit(),
child: MaterialApp(
theme: ThemeData.dark(),
home: SignUpScreen(),
routes: {
SignInScreen.id: (context) => SignInScreen(),
SignUpScreen.id: (context) => SignUpScreen(),
PostsScreen.id: (context) => PostsScreen(),
},
),
);
}
}
Я пытался создать нового пользователя в своем приложении, но экран зависает при загрузке.
Подробнее здесь:
https://stackoverflow.com/questions/792 ... ing-button
1734293038
Anonymous
Мое приложение Flutter, использующее Firebase, зависает на экране загрузки после нажатия кнопки. Код кнопки: [code]TextButton(onPressed: (){ _submit(context); }, child: Text("Sign In") ), [/code] [code] void _submit(BuildContext context){ FocusScope.of(context).unfocus(); if(!_formKey.currentState!.validate()){ //Invalid return; } _formKey.currentState!.save(); context.read().signUp(email: _email, username: _username, password: _password); } [/code] Настроил параметры подключения к Firebase по инструкции. Есть такое предупреждение: [code]Launching lib\main.dart on sdk gphone64 x86 64 in debug mode... Running Gradle task 'assembleDebug'... **Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.** √ Built build\app\outputs\flutter-apk\app-debug.apk [/code] для быстрого воспроизведения: sign_up_screen.dart [code]import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:social_network/bloc/auth_cubit.dart'; import 'package:social_network/screens/posts_screen.dart'; import 'package:social_network/screens/sign_in_screen.dart'; class SignUpScreen extends StatefulWidget { static const String id = "sign up screen"; const SignUpScreen({super.key}); @override State createState() => _SignUpScreenState(); } class _SignUpScreenState extends State { final _formKey = GlobalKey(); String _email = ""; String _username = ""; String _password = ""; late final FocusNode _passwordFocusNode; late final FocusNode _usernameFocusNode; @override void initState() { _passwordFocusNode = FocusNode(); _usernameFocusNode = FocusNode(); super.initState(); } @override void dispose() { _passwordFocusNode.dispose(); _usernameFocusNode.dispose(); super.dispose(); } void _submit(BuildContext context){ FocusScope.of(context).unfocus(); if(!_formKey.currentState!.validate()){ //Invalid return; } _formKey.currentState!.save(); context.read().signUp(email: _email, username: _username, password: _password); } @override Widget build(BuildContext context) { return Scaffold( body: BlocConsumer( listener: (prevState, currState){ if(currState is AuthSignedUp){ Navigator.of(context).pushReplacementNamed(PostsScreen.id); } if(currState is AuthFailure){ ScaffoldMessenger.of(context).showSnackBar( SnackBar( duration: Duration(seconds: 2), content: Text(currState.message))); } }, builder: (context, state){ if(state is AuthLoading){ return Center(child: CircularProgressIndicator()); } return SafeArea( child: Form( key: _formKey, child: Padding( padding: const EdgeInsets.all(15.0), child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ Padding( padding: const EdgeInsets.symmetric(vertical: 18.0), child: Text("Social Media App", style: Theme.of(context).textTheme.headlineLarge,), ), SizedBox(height: 15,), // email TextFormField( keyboardType: TextInputType.emailAddress, decoration: InputDecoration( enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.black), ), labelText: "Enter you emaail", ), textInputAction: TextInputAction.next, onFieldSubmitted: (_){ FocusScope.of(context).requestFocus(_usernameFocusNode); }, onSaved: (value){ _email = value!.trim(); }, validator: (value){ if(value!.isEmpty){ return "Please enter your email"; } return null; }, ), // username SizedBox(height: 15,), TextFormField( focusNode: _usernameFocusNode, decoration: InputDecoration( enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.black), ), labelText: "Enter you username", ), textInputAction: TextInputAction.next, onFieldSubmitted: (_){ FocusScope.of(context).requestFocus(_passwordFocusNode); }, onSaved: (value){ _username = value!.trim(); }, validator: (value){ if(value!.isEmpty){ return "Please enter your username"; } return null; }, ), // password SizedBox(height: 15,), TextFormField( focusNode: _passwordFocusNode, decoration: InputDecoration( enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.black), ), labelText: "Enter you password", ), obscureText: true, textInputAction: TextInputAction.done, onFieldSubmitted: (_){ _submit(context); }, onSaved: (value){ _email = value!.trim(); }, validator: (value){ if(value!.isEmpty){ return "Please enter your password"; } if(value.length < 5){ return "Please enter your password more than 5 characters"; } return null; }, ), SizedBox(height: 15,), TextButton(onPressed: (){ _submit(context); }, child: Text("Sign Up") ), TextButton(onPressed: (){ Navigator.of(context).pushReplacementNamed(SignInScreen.id); }, child: Text("Log In") //Sign In instead ) ], ), ), ), ) ); } ), ); } } [/code] main.dart [code]import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:social_network/bloc/auth_cubit.dart'; import 'package:social_network/screens/posts_screen.dart'; import 'package:social_network/screens/sign_in_screen.dart'; import 'package:social_network/screens/sign_up_screen.dart'; import 'package:firebase_core/firebase_core.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return BlocProvider( create: (context) => AuthCubit(), child: MaterialApp( theme: ThemeData.dark(), home: SignUpScreen(), routes: { SignInScreen.id: (context) => SignInScreen(), SignUpScreen.id: (context) => SignUpScreen(), PostsScreen.id: (context) => PostsScreen(), }, ), ); } } [/code] Я пытался создать нового пользователя в своем приложении, но экран зависает при загрузке. Подробнее здесь: [url]https://stackoverflow.com/questions/79277402/my-flutter-app-using-firebase-gets-stuck-on-loading-screen-after-pressing-button[/url]