我是 flutter 的初学者,是否有通过在 Flutter 中使用 flutter_bloc 包在一页中与多 blocbuilder 相关的示例或文档。
我正在尝试找到一种方法来创建块的新实例并将其反映在多块提供程序中。
目前我有以下内容:
Scaffold(
appBar: AppBar(),
body: MultiBlocProvider(
providers: [
BlocProvider<BlocABloc>(
create: (BuildContext context) => _aBloc,
),
BlocProvider<BlocBBloc>(
create: (BuildContext context) => _bBloc,
),
]...
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建 BlocABloc 和 BlocBBloc 的新实例:
generateNew(){
setState(() {
_aBloc = BlocABloc();
_bBloc = BlocBBloc();
});
}
Run Code Online (Sandbox Code Playgroud)
我期望构建函数重新执行,并在 BlocProvider 中使用新实例。但是,我发现 BlocBuilder 仍在从 Bloc 的前一个实例获取状态。
有没有办法处理这种情况?
在构建器方法中,我达到了状态值,例如
\nreturn BlocBuilder<UsersOwnProfileBloc, UsersOwnProfileState>(\n cubit: widget.bloc,\n builder: (context, state) {\n if (state is FetchedUserSettingState) {\n bool account = state.userSettings.publicAccount\n}\n\nRun Code Online (Sandbox Code Playgroud)\n但我需要从 initState 获取值。我需要设置小部件的值。我尝试了类似的方法,但出现错误
\n @override\n void initState() {\n super.initState();\n UsersOwnProfileState state = BlocProvider.of<UsersOwnProfileBloc>(context).state;\n if (state is FetchedUserSettingState) {\n publicAccount = state.userSettings.publicAccount;\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n谁能告诉我如何获取 initState 中的状态值?
\nclass UserSettingPage extends StatefulWidget {\n final UsersOwnProfileBloc bloc;\n\n const UserSettingPage({Key key, this.bloc}) : super(key: key);\n\n @override\n _UserSettingPageState createState() => _UserSettingPageState();\n}\n\nclass _UserSettingPageState extends State<UserSettingPage> {\n bool newListingAlert;\n bool listingForSearchAlert;\n bool searchForListingAlert;\n bool …Run Code Online (Sandbox Code Playgroud) 我正在Flutter Bloc一个项目中使用。
这是结构:
MultiBlocProvider(
providers: [
BlocProvider<BlocA>(
create: (context) {
return BlocA()
..add(FetchEvent());
},
),
BlocProvider<BlocC>(
create: (context) {
return BlocC()..add(FetchEvent());
},
),
],
child: IndexedStack(
index: _pageNum,
children: [
ChildA(),
ChildB(),
],
),
)
Run Code Online (Sandbox Code Playgroud)
里面ChildB我有 aNavigator.push()到 a ChildC,BlocC正在使用 ( BlocConsumer),但我收到错误,
Flutter could not find the correct Provider above BlocConsumer<BlocC> Widget
Run Code Online (Sandbox Code Playgroud)
编辑
有一个按钮可以在按下时ChildB导航到。ChildC喜欢,
TextButton( // this code exists inside a scaffold widget
child: Text("Page C"),
onPressed: …Run Code Online (Sandbox Code Playgroud) 因此,我正在使用 Django Rest 框架创建我的 API 等。我想检查是否可以从后端得到响应。所以,我能够得到回应。但我在调试控制台上看到了一些问题。请帮我找出问题所在。这是我在调试控制台上收到的信息。
Restarted application in 1,314ms.
I/flutter ( 1751): {"token":"0d15f2d45f11869174395d623c066080bd2ade52"}
E/flutter ( 1751): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 'package:bloc/src/bloc.dart': Failed assertion: line 232 pos 7: '!_isCompleted':
E/flutter ( 1751):
E/flutter ( 1751):
E/flutter ( 1751): emit was called after an event handler completed normally.
E/flutter ( 1751): This is usually due to an unawaited future in an event handler.
E/flutter ( 1751): Please make sure to await all asynchronous operations with event handlers
E/flutter ( 1751): and use …Run Code Online (Sandbox Code Playgroud) 尝试在 android Studio 中运行以下 Flutter 代码
链接到 github 上的代码 https://github.com/cyeh1234/bloc_bug.git
出现以下错误:
https://www.mitrais.com/news-updates/getting-started-with-flutter-bloc-pattern/
正确输出
我是 flutter 中的 Bloc 新手,任何人都可以解释一下我何时应该在 Blocs 中使用 add() 以及何时使用 emmit ?
我在 user_bloc.dart 中有这段代码:
\nUserBloc(this._userRepository, UserState userState) : super(userState) {\n on<RegisterUser>(_onRegisterUser);\n }\n\nvoid _onRegisterUser(RegisterUser event, Emitter<UserState> emit) async {\n emit(UserRegistering());\n try {\n // detect when the user is registered\n FirebaseAuth.instance.authStateChanges().listen((User? user) async {\n if (state is UserRegistering && user != null) {\n // save user to db\n try {\n await _userRepository.addUpdateUserInfo(user.uid, userInfo);\n } catch (e) {\n emit(UserRegisteringError("Could not save user"));\n }\n emit(UserLoggedIn(user));\n }\n });\n ... call FirebaseAuth.instance.createUserWithEmailAndPassword\nRun Code Online (Sandbox Code Playgroud)\n这是 _userRepository.addUpdateUserInfo:
\nFuture<void> addUpdateUserInfo(String userId, UserInfo userInfo) async {\n try {\n var …Run Code Online (Sandbox Code Playgroud) 除了可追溯性(您也可以通过 Cubit 中的适当日志记录来实现)和高级事件转换(我想不出 Cubit 无法做到的任何“高级”事件转换,因为总有办法做到这一点)使用 Cubit。如果您使用干净的架构,域/数据层可以帮助进行复杂的数据操作)。
这些是我正在寻找的应该能够用 Bloc 完成的事情,因为这些事情实际上不能用 Cubit 完成。然而,这些似乎是不可能的(或者是吗?),因为在 Bloc 上添加事件需要您识别将添加事件的实际 Bloc。bloc.add(YourEvent())。
此外,事件共享有些争议,因为这可能会导致糟糕的架构/难以维护。
对于事件溯源,我无法在文档中找到这是否可能(返回到特定的过去状态?)。
我在这里错过了什么吗?
event-driven-design flutter clean-architecture bloc flutter-bloc
我有这个代码:
MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => CubitExample(),
),
],
child: MaterialApp(
home: Home(),
),
);
Run Code Online (Sandbox Code Playgroud)
它工作正常,我有很多肘节/块,所以我决定将提供者列表移动到一个单独的类:
MultiBlocProvider(
providers: BlocProviders.blocs(),
child: MaterialApp(
home: Home(),
),
);
Run Code Online (Sandbox Code Playgroud)
// The class
class BlocProviders {
static List<BlocProvider> blocs() {
return <BlocProvider>[
BlocProvider(
create: (context) => CubitExample(()),
),
];
}
}
Run Code Online (Sandbox Code Playgroud)
现在抛出通常的块错误:
ProviderNotFoundException (Error: Could not find the correct Provider<ExampleCubit> above this BlocBuilder<ExampleCubit, ExampleState> Widget
Run Code Online (Sandbox Code Playgroud)
我不知道为什么当我尝试第二个代码时会抛出错误,为什么会抛出错误?