我有一个集团层次结构,在子集团中mapEvenToState我使用了super.mapEventToState. 在 bloc 包的较新版本中,mapEventToState已弃用。
我应该用什么来代替super.mapEventToState?我知道on<Event>,但是相当于什么super.mapEventToState?
我正在使用flutter_bloc包。如果我有一个状态是小部件列表的块,这是一个不好的做法吗?我使用这个块从列表中推送(添加)和弹出(删除)小部件/迷你屏幕。我将此列表用作弹出菜单的主体,其中有类似嵌入式导航的内容。列表的最后一个成员是显示在弹出窗口中的小部件。
每次我按下或弹出时,我都会发出一个新的状态。该块很有用,因为这样我就可以从弹出窗口中显示的小部件/迷你屏幕中的任何位置调用推送或弹出。如果我的用例很清楚或者您需要更多详细信息,请告诉我。谢谢。
以下是相关代码片段:
自定义堆栈(其中E类型为Widget):
class PopoverStack<E> {
PopoverStack() : _storage = <E>[];
final List<E> _storage;
void push(E element) {
_storage.add(element);
}
void pop() {
_storage.removeLast();
}
E get last => _storage.last;
bool get isEmpty => _storage.isEmpty;
bool get isNotEmpty => !isEmpty;
PopoverStack.of(PopoverStack<E> stack) : _storage = List<E>.of(stack._storage);
}
Run Code Online (Sandbox Code Playgroud)
Bloc for stack(PopoverPage是一个抽象类小部件将扩展):
class PopoverCardStackBloc extends Cubit<PopoverStack<PopoverPage>> {
PopoverCardStackBloc(PopoverStack<PopoverPage> popoverStack) : super(popoverStack);
void push(PopoverPage element) {
emit(PopoverStack.of(state..push(element)));
}
void pop() {
emit(PopoverStack.of(state..pop()));
}
} …Run Code Online (Sandbox Code Playgroud) 使用 flutter_bloc 和 go_router 实现基于身份验证更改的导航。
我需要做的是,如果用户经过身份验证,则应将其重定向到主屏幕,并能够导航身份验证流程...(HomeScreen,TestScreen1,TestScreen2)
如果用户在任何时候未经身份验证,则应将其重定向到登录屏幕,如果未知,则应重定向到启动屏幕。
我已经解决了许多 stackoverflow 问题和 github 问题,但没有一个提供我们如何实现这一目标的最佳实践。
问题:
提供以下资产:
Tried to listen to a value exposed with provider, from outside of the widget tree.
This is likely caused by an event handler (like a button's onPressed) that called
Provider.of without passing `listen: false`.
To fix, write:
Provider.of<$T>(context, listen: false);
It is unsupported because may pointlessly rebuild the widget associated to the
event handler, when the widget tree …Run Code Online (Sandbox Code Playgroud) 我正在使用 Flutter BLOC 库(https://pub.dev/packages/bloc)我知道有一种方法可以“监听”BLOC 状态更改(使用 Listen() 函数)
chatBloc.listen((chatState) async {
if (chatState is ChatStateInitialized) {
// do something
}
});
Run Code Online (Sandbox Code Playgroud)
但是有没有办法来监听 BLOC 事件呢?就像我对经典 StreamController 所做的那样?感谢所有愿意提供帮助的人:-)
朱利安
我正在使用 flutter 完成许多关于 bloc 的教程,但遇到了一些不一致的情况。
我正在使用 Android studio 并使用 Intellij v1.6.0 的插件创建块代码。
对于 bloc_event,我继续看到类似这样的示例。
@immutable
abstract class FruitEvent extends Equatable {
FruitEvent([List props = const []]) : super(props);
}
Run Code Online (Sandbox Code Playgroud)
当我生成 bloc 文件并查看生成的初始 _event 文件时,它看起来像这样。
@immutable
abstract class SongEvent extends Equatable {
const SongEvent();
}
Run Code Online (Sandbox Code Playgroud)
如果我修改生成的代码以包含以下内容...
[List props = const []]) : super(props)
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误“位置参数太多,预期 0 个,找到 1 个”,该错误引用了上面显示的行末尾的 props。
如果我保留由 bloc 插件生成的代码,然后尝试通过添加以下内容来实现我的事件...
class AddSong extends SongEvent {}
Run Code Online (Sandbox Code Playgroud)
然后我收到错误“缺少‘getter Equatable.props’的具体实现”
这是我当前的 bloc/song_event.dart
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
@immutable
abstract class SongEvent extends Equatable …Run Code Online (Sandbox Code Playgroud) 我在使用flutter_bloc在 Flutter 上实现应用程序时遇到问题。我理解核心概念,但我发现了一个“边缘情况”,其中没有示例或指南(至少我能找到):
(我将简化问题)我有一个称为AuthBloc管理App. 如果状态为 ,NotAuthenticated则应用程序应显示 ,LoginScreen但如果状态为 ,则应用Authenticated程序应显示HomeScreen。在HomeScreenI 中有 4 个块,其中每个块都有它的状态和事件,但它们都依赖于不同的Repositories从 API 获取一些数据。
所有这些都Repositories需要一个令牌来发出 API 请求。第一个问题就到这里了。如何从所有存储库中获取令牌?如果我使用 aUserRepository来存储令牌,则需要将其作为依赖项传递给每个存储库(可能有效,但我认为这不是正确的方法)。那么,什么是管理这个的正确方法呢?
第二个问题是:
如果以某种方式我可以在所有存储库查询中获取令牌,那么在撤销令牌时会发生什么?应用程序应该返回到LoginScreen,为此我需要AuthBloc通过事件(例如InvalidTokenEvent)通知。并且AuthBloc应该将其状态更改为NotAuthenticated,这将重建LoginScreen. 但问题是:我如何AuthBloc从其他集团或存储库通知?我的第一个想法是通过依赖注入:我可以将 AuthBloc 传递给构造函数中的所有其他块,因此当存储库请求返回令牌过期时,XBloc可以调用AuthBloc.add(InvalidTokenEvent). 但同样,如果我有很多集团,我需要在每个集团中都这样做。那么,这样做的正确方法是什么?
感谢您的任何帮助!
我正在使用 Flutter 开发移动应用程序。我正在使用 flutter 块包https://pub.dev/packages/flutter_bloc来管理和设置块。但是当状态改变时,它不会更新小部件或视图。
我有一个名为 home_bloc.dart 的 bloc 类文件,具有以下实现。
class HomeEvent {
static const int FETCH_ARTICLES = 1;
static const int TOGGLE_IS_FILTERING = 2;
int _event = 0;
String _filterKeyword = "";
int get event => _event;
void set event(int event) {
this._event = event;
}
String get filterKeyword => _filterKeyword;
void set filterKeyword(String filterKeyword) {
this._filterKeyword = filterKeyword;
}
}
class HomeBloc extends Bloc<HomeEvent, HomeState> {
Repository _repository = Repository();
HomeState state = HomeState();
@override
HomeState …Run Code Online (Sandbox Code Playgroud) 我正在使用该包flutter_bloc进行状态管理。我想创建一个搜索屏幕,并找到了Flutter 函数,但在向我的实现创建的实例showSearch提供实例时遇到了问题。我终于成功了,但想问一下最好的方法是什么。这是代码(摘录,从放置在a 中的按钮开始):BLoCListViewSearchDelegateAppBarScaffold
class ItemSearchButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return IconButton(
icon: Icon(Icons.search),
onPressed: () {
final itemListBloc = context.bloc<ItemListBloc>();
showSearch(
context: context,
delegate: _ItemSearchDelegate(itemListBloc),
);
},
);
}
}
class _ItemSearchDelegate extends SearchDelegate<String> {
final ItemListBloc itemListBloc;
_ItemSearchDelegate(this.itemListBloc);
// other overridden methods
@override
Widget buildSuggestions(BuildContext context) {
return BlocProvider.value(
value: itemListBloc,
child: ItemListWidget(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,调用该方法的上下文showSearch具有正确的 BLoC 实例,但它在我的实现中不可用SearchDelegate,除非我在buildSuggestions.
为什么 …
下面是代码。
ElevatedButton(
onPressed: () => Navigator.of(context, rootNavigator: true)
.push(MaterialPageRoute(
fullscreenDialog: true,
builder: (context) => UserLoginPage(),
)),
child: Text('Login to continue'),
),
Run Code Online (Sandbox Code Playgroud)
登录页面内:
BlocConsumer<UserAuthCubit, UserAuthState>(
listener: (context, state) {
if (state is UserAuthorized) {
Navigator.of(context, rootNavigator: true).pop();
}
if (state is UserAuthWaiting) {
showModalBottomSheet(
useRootNavigator: true,
isDismissible: false,
context: context,
builder: (context) {
return WillPopScope(
onWillPop: () async => false,
child: Center(
child: Text(state.msg),
),
);
});
dialog = true;
} else {
if (dialog) {
Navigator.of(context, rootNavigator: true).pop();
dialog …Run Code Online (Sandbox Code Playgroud) 我是 Dart 语言开发的初学者。我尝试创建一个受此GitHub repo启发的示例 Flutter 应用程序 BLOC 模式,但我收到了一些与类继承相关的错误消息。我已经熟悉点网C#语言中的继承和超类和子类编程。但就飞镖而言,我需要一些建议。
这是我的代码:
class UserRegBloc extends Bloc<UserRegEvent, UserRegState> {
UserRepository userRepository;
UserRegBloc({@required UserRepository userRepository}) {
userRepository = UserRepository();
}
@override
UserRegState get initialState => UserRegInitial();
@override
Stream<UserRegState> mapEventToState(UserRegEvent event) async* {
if (event is SignUpButtonPressed) {
yield UserRegLoading();
try {
var user = await userRepository.signUpUserWithEmailPass(
event.email, event.password);
print("BLOC : ${user.email}");
yield UserRegSuccessful(user: user);
} catch (e) {
yield UserRegFailure(message: e.toString());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 pubspec.yaml 依赖项如下: