使用BLoC模式的Flutter登录系统 [英] Flutter login system using BLoC pattern

查看:58
本文介绍了使用BLoC模式的Flutter登录系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:我对Flutter和Dart还是很陌生,我正试图为自己做一种有关如何执行登录并保护我的应用页面的练习strong>.

我问这个问题的目的是了解有关保护,登录和注销Flutter应用程序的最佳实践.

我已经对可用的体系结构和模式进行了大量研究,并且阅读了有关BLoC模式的信息,但是我仍然很难理解它的工作原理.

如果有人可以帮助我解释如何处理应用程序会话(例如,当我从NodeJS后端返回JWT时),如何存储它们并在应用程序页面之间共享它们的状态如果我成功登录,该如何检测到这个新会话并将用户推送到新页面?

我尝试过的操作:我已经在Flutter上实现了一些发现"功能,但实现了一些StreamController,但我没有在此处放置相关代码.

欢迎任何输入或阅读.

谢谢,如果我的问题不是很好,我恳请您帮助我改善问题.

解决方案

有逐步登录BLoC教程

登录BLoC的代码段

  import'package:flutter/material.dart';导入'package:bloc/bloc.dart';导入'package:flutter_bloc/flutter_bloc.dart';导入'package:user_repository/user_repository.dart';导入'package:flutter_login/authentication/authentication.dart';导入'package:flutter_login/splash/splash.dart';导入'package:flutter_login/login/login.dart';导入'package:flutter_login/home/home.dart';导入'package:flutter_login/common/common.dart';类SimpleBlocDelegate扩展了BlocDelegate {@overridevoid onEvent(Bloc bloc,Object event){super.onEvent(bloc,event);打印(事件);}@overridevoid onTransition(Bloc bloc,Transition transition){super.onTransition(bloc,transition);打印(转换);}@overridevoid onError(Bloc bloc,Object error,StackTrace stacktrace){super.onError(bloc,错误,堆栈跟踪);打印(错误);}}void main(){BlocSupervisor.delegate = SimpleBlocDelegate();最终的userRepository = UserRepository();runApp(BlocProvider< AuthenticationBloc>(生成器:(上下文){返回AuthenticationBloc(userRepository:userRepository)..add(AppStarted());},子代:App(userRepository:userRepository),),);}App类扩展了StatelessWidget {最终的UserRepository userRepository;App({Key key,@required this.userRepository}):超级(key:key);@override窗口小部件build(BuildContext context){返回MaterialApp(主页:BlocBuilder< AuthenticationBloc,AuthenticationState>(生成器:(上下文,状态){如果(状态为AuthenticationUninitialized){返回SplashPage();}如果(状态为AuthenticationAuthenticated){返回HomePage();}如果(状态为AuthenticationUnauthenticated){返回LoginPage(userRepository:userRepository);}如果(状态为AuthenticationLoading){返回LoadingIndicator();}},),);}} 

Summary: I'm very new on Flutter and Dart and I'm trying to create a kind of exercise for myself about how to perform a login and protect my app pages.

My goal asking this question is to understand about the best practices to protect, login and logout from my Flutter app.

I've performed a lot of research about the architectures and patterns available and I've read about the BLoC pattern but I still have difficult to understand how it works.

If someone could help me with some explanation about how can I deal with the app sessions (when I have a JWT for example returned from my NodeJS backend), how can I store them and share their state among the pages of my application and if I have a successfully login how can I detect this new session and push my user to a new page?

What I've tried: I've implemented some StreamControllers on a kind of "discovering" on Flutter but I don't have a relevant code to place here.

Any input or good reading are welcome.

Thanks and if my question was not so good, I kindly ask for you to help me to improve it.

解决方案

There is a step by step login BLoC Tutorial https://bloclibrary.dev/#/flutterlogintutorial?id=setup
And also Weather , ToDo , Firebase login, Timer you can reference
This Tutorial use package flutter_bloc and have complete code

code snippet for Login BLoC

import 'package:flutter/material.dart';

import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:user_repository/user_repository.dart';

import 'package:flutter_login/authentication/authentication.dart';
import 'package:flutter_login/splash/splash.dart';
import 'package:flutter_login/login/login.dart';
import 'package:flutter_login/home/home.dart';
import 'package:flutter_login/common/common.dart';

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final userRepository = UserRepository();
  runApp(
    BlocProvider<AuthenticationBloc>(
      builder: (context) {
        return AuthenticationBloc(userRepository: userRepository)
          ..add(AppStarted());
      },
      child: App(userRepository: userRepository),
    ),
  );
}

class App extends StatelessWidget {
  final UserRepository userRepository;

  App({Key key, @required this.userRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is AuthenticationUninitialized) {
            return SplashPage();
          }
          if (state is AuthenticationAuthenticated) {
            return HomePage();
          }
          if (state is AuthenticationUnauthenticated) {
            return LoginPage(userRepository: userRepository);
          }
          if (state is AuthenticationLoading) {
            return LoadingIndicator();
          }
        },
      ),
    );
  }
}

这篇关于使用BLoC模式的Flutter登录系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆