【发布时间】:2021-11-02 18:17:43
【问题描述】:
这是一个主要关于最佳实践的问题,我是 Flutter 开发和 BLoC 架构的新手,总而言之,我有一个应用程序,它有一个 app_bloc 处理用户身份验证(firebase 登录),它非常简单,有两种状态 Authenticated 和 Unauthenticated。
我的应用根据用户是否通过身份验证来呈现页面,无论是登录页面还是主页。
import 'package:authentication_repository/authentication_repository.dart';
import 'package:flow_builder/flow_builder.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:majstor/app/bloc/app_bloc.dart';
import 'package:majstor/app/routes/routes.dart';
import 'package:majstor/theme.dart';
class App extends StatelessWidget {
const App({
Key? key,
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
super(key: key);
final AuthenticationRepository _authenticationRepository;
@override
Widget build(BuildContext context) {
return RepositoryProvider.value(
value: _authenticationRepository,
child: BlocProvider(
create: (_) => AppBloc(
authenticationRepository: _authenticationRepository,
),
child: const AppView(),
),
);
}
}
class AppView extends StatelessWidget {
const AppView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
/// try context.select here maybe?
return MaterialApp(
home: FlowBuilder<AppStatus>(
state: context.select((AppBloc bloc) => bloc.state.status),
onGeneratePages: onGenerateAppViewPages,
),
);
}
}
import 'package:flutter/widgets.dart';
import 'package:majstor/app/app.dart';
import 'package:majstor/home/home.dart';
import 'package:majstor/login/login.dart';
List<Page> onGenerateAppViewPages(AppStatus state, List<Page<dynamic>> pages) {
switch (state) {
case AppStatus.authenticated:
return [HomePage.page()];
case AppStatus.unauthenticated:
default:
return [LoginPage.page()];
}
}
现在我很难理解的是,在我的主页案例中,我想进一步将我的用户“拆分”为角色,Firebase 集合中将存储两个角色用户在同一文档上的相应 id,并且对于每个角色,应该完全构建一个不同的页面,我不确定我将在哪里进行角色检查,我可以在技术上在下面的主页中,使用查询数据库用户 ID,并获取角色,然后像我对登录和主页所做的那样,使用 flowbuilder 根据角色生成不同的页面?但这是一个好习惯吗?我觉得我应该将数据库逻辑分离到其他地方,但由于我是这种开发的新手,我不知道我应该在哪里做这个来构建一个可扩展的应用程序。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:majstor/app/app.dart';
import 'package:majstor/home/home.dart';
import 'package:majstor/utils/databaseservice.dart';
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
static Page page() => const MaterialPage<void>(child: HomePage());
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
final user = context.select((AppBloc bloc) => bloc.state.user);
Database db = Database();
db.readData(user.id);
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
actions: <Widget>[
IconButton(
key: const Key('homePage_logout_iconButton'),
icon: const Icon(Icons.exit_to_app),
onPressed: () => context.read<AppBloc>().add(AppLogoutRequested()),
)
],
),
body: Align(
alignment: const Alignment(0, -1 / 3),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Avatar(photo: user.photo),
const SizedBox(height: 4),
Text(user.email ?? '', style: textTheme.headline6),
const SizedBox(height: 4),
Text(user.name ?? '', style: textTheme.headline5),
],
),
),
);
}
}
我是否在上面的文件中查询数据库,并在那里进行检查,或者有没有办法进一步分离我的逻辑?我是否也应该将 BloC 用于角色?还是我应该以某种方式将角色整合到我现有的 BloC 中?希望我的解释足够清楚。
【问题讨论】:
标签: firebase flutter dart architecture bloc