- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须用 Flutter 开发一个简单的应用程序,我第一次尝试使用 bloc 模式。我正在使用 flutter_bloc
包,但我有两个主要问题并且卡住了:
authentication_bloc
获得 token (登录),但我不能将该 token 共享给所有集团,除非我让它们依赖于 auth_bloc(拥有存储 token 的用户模型的集团)。对于一个简单的应用程序,也许它可以工作,但如果其他 bloc 除了 auth_bloc 之外还有更多 bloc 依赖项,那么将所有 bloc 作为依赖项注入(inject)可能会变得一团糟。什么替代方案可能是一个好的解决方案? bloc
用于底部导航栏。但是我想所有的标签页上的屏幕都可以调用change_tab()
功能在底部栏 block 。同样是同样的问题,如果其他集团对底栏集团有依赖关系,我只能在底栏集团添加事件。我不想将身份验证 block 和底栏 block 添加为对管理所有屏幕的所有 block 的依赖项。 最佳答案
您可以将 token 存储在 Repository
如userRepository.persistToken(event.token)
和 await userRepository.deleteToken();
存储库的行为类似于存储库模式。您不需要知道数据来自云或本地数据库
在下面的示例中,userRepository 可以是应用程序级别,因此所有 bloc 都可以使用它
完整示例代码 https://github.com/felangel/bloc/blob/master/examples/flutter_login/lib/authentication/authentication_bloc.dart
代码片段
void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
final userRepository = UserRepository();
runApp(
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(userRepository: userRepository)
..add(AppStarted());
},
child: App(userRepository: userRepository),
),
);
}
...
import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'package:user_repository/user_repository.dart';
import 'package:flutter_login/authentication/authentication.dart';
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
final UserRepository userRepository;
AuthenticationBloc({@required this.userRepository})
: assert(userRepository != null);
@override
AuthenticationState get initialState => AuthenticationUninitialized();
@override
Stream<AuthenticationState> mapEventToState(
AuthenticationEvent event,
) async* {
if (event is AppStarted) {
final bool hasToken = await userRepository.hasToken();
if (hasToken) {
yield AuthenticationAuthenticated();
} else {
yield AuthenticationUnauthenticated();
}
}
if (event is LoggedIn) {
yield AuthenticationLoading();
await userRepository.persistToken(event.token);
yield AuthenticationAuthenticated();
}
if (event is LoggedOut) {
yield AuthenticationLoading();
await userRepository.deleteToken();
yield AuthenticationUnauthenticated();
}
}
}
MultiBlocProvider
在应用程序级别包装你的 block
void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
runApp(
MultiBlocProvider(
providers: [
BlocProvider<MapBloc>(
create: (context) => MapBloc(),
),
BlocProvider<PoisBloc>(
create: (context) => PoisBloc(
mapBloc: BlocProvider.of<MapBloc>(context),
),
),
],
child: App(),
)
);
}
class App extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bet Over',
theme: appTheme,
initialRoute: '/',
routes: {
'/': (context) =>
MultiBlocProvider(
providers: [
BlocProvider<MapBloc>(
create: (context) => BlocProvider.of<MapBloc>(context),
),
BlocProvider<PoisBloc>(
create: (context) => BlocProvider.of<PoisBloc>(context),
),
],
child: MapPage(),
),
'/create-poi': (context) =>
MultiBlocProvider(
providers: [
BlocProvider<MapBloc>(
create: (context) => BlocProvider.of<MapBloc>(context),
),
BlocProvider<PoisBloc>(
create: (context) => BlocProvider.of<PoisBloc>(context),
),
],
child: CreatePoimPage(),
),
}
);
}
}
class _MapState extends State<Map> {
MapBloc _mapBloc;
PoisBloc _poisBloc;
@override
void initState() {
super.initState();
_mapBloc = BlocProvider.of<MapBloc>(context);
_poisBloc = BlocProvider.of<PoisBloc>(context);
...
List<Marker> _generateMarkers () {
if (!(_mapBloc.state is MapViewboxChanged && _poisBloc.state is PoisLoaded))
return [];
关于flutter - 使用 flutter_bloc 构建 Flutter App 的架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59621809/
所以,我正在使用 Django REST 框架创建我的 API 和所有内容。我想检查我是否可以从后端获得响应。所以,我能够得到回应。但是我在调试控制台上看到了一些问题。请帮我找出问题所在。这是我在
我在使用 flutter_bloc 在 Flutter 上实现应用程序时遇到问题.我理解核心概念,但我发现了一个“边缘情况”,其中没有示例或指南(至少我能找到): (我会简化问题)我有一个叫 Auth
我正在用BLoC模式在Flutter中重写一个非常简单的应用程序。但是,当我尝试在应用程序中使用BLoC组件时,出现错误。 我不知道该如何解决,如果有人知道,我将非常高兴! 当我尝试在BlocList
我正在使用 flutter_bloc 库来构建我的应用程序。除了 BlocProvider 之外,我还使用存储库提供程序,因为我将在整个应用程序中广泛使用特定的存储库。但我在上下文方面遇到了问题。以下
我正在尝试 Flutter,我在 github 上提出了许多项目,这些项目声明了存储库并将它们传递给 UI 端的 bloc。 为什么这是正确的做法,而不是在后端(在集团)? 例子: class MyA
BLoC 模式的实现有很多版本。其中之一是 Felix Angelov 的 flutter_bloc。 在其中一个社交媒体上,我看到了 flutter_bloc 的声明 对项目来说不是一个好的选择,应
我正在尝试创建一个 BLOC,它依赖于另外两个基于时间的 bloc 和一个非基于时间的 bloc。我所说的基于时间的意思是,例如,他们正在连接远程服务器,所以这需要时间。它的工作原理是这样的: 登录(
我正在使用flutter_bloc:^6.1.1以及依赖注入(inject)get_it:^5.0.6。当我向集团提交事件时,状态没有改变。 nav_bar_bloc.dart import 'dar
我正在使用 flutter_bloc 包来管理我的应用程序中的状态。我有一个用例,我必须从远程数据库加载初始状态。这要求 initialState 方法是异步的,但它不是。 如果不是通过使用 init
假设我有一个带有嵌套集合的 firestore 数据库。一所学校可以有不同的类(class),每门类(class)有不同的部分,每个部分有不同的页面。真的很简单。 从 Firestore 模式的角度来
我已经使用flutter_bloc创建了一个BLoC,在其中我可以收听流。当父窗口小部件(以及BLoC对象)被释放时,我想关闭流。 class ChatBloc extends Bloc { //
我有一个 tabview,我有流行的、最近的和即将到来的类别。他们在 api 上都有相同的响应。我正在尝试使用 flutter_bloc 从 api 获取数据。以前我使用 rxdart 主题,我为每种
假设我想在每次调用 Api 时检查互联网连接,如果没有互联网,调用会抛出异常,例如 NoInternetException,然后向用户显示一个状态屏幕,告诉他检查他们的连接。 如果不为 flutter
我是 Flutter 的新手,目前正在研究 DI。 我正在使用 flutter_bloc 和 provider 包。 flutter_bloc 附带一个 RepositoryProvider,我现在问
我是 Flutter 的新手,目前正在研究 DI。 我正在使用 flutter_bloc 和 provider 包。 flutter_bloc 附带一个 RepositoryProvider,我现在问
我最近在 youtube ( https://pub.dev/packages/flutter_bloc ) 上观看了 felix Angelov 的 flutter_bloc 包 (https://
我在 flutter 中遇到了附加的小部件测试问题。当我单独运行测试时,每个测试都成功了;但是,当我运行整个 main() 方法时,前三个测试成功,但后两个测试失败,并出现以下异常: Expected
为什么当我们需要使用带有 flutter_bloc 的 Equatable 类时? ,还有我们需要的 Prop 呢?这是在 flutter 中以块模式创建状态的示例代码,请我需要详细的答案。先感谢您
我正在Flutter中开始使用Bloc。任何人都可以告诉我什么是真正的“flutter_bloc”和“bloc”软件包。 是相同的。 何时/如何使用它。 谢谢 最佳答案 “bloc”包包含您将在Blo
我有一个 flutter 应用程序,在其中我在 listView 小部件中显示员工列表及其姓名和销售额。我为每个员工提供了两个按钮来增加或减少他们的销售额。 我正在使用flutter_bloc用于状态
我是一名优秀的程序员,十分优秀!