我开始使用颤振。这看起来很不错也很容易,但是在错误方面没有太多资源。无论如何,经过长时间的思考、检查和即兴创作,我开始在网上搜索答案,嗯,没有。所以我想问你们所有人,也许有人能够向我解释为什么我会收到这个错误,它意味着什么以及如何摆脱它。
哦,在我深入探讨之前,我可能应该提到我正在使用 flutter_bloc。
好的,所以我有这样的代码:
class Settings extends StatelessWidget {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("SomeApp",style: TextStyle(color: Colors.white)),
automaticallyImplyLeading: false,
backgroundColor: myBlue.shade50,
actions: <Widget>[
IconButton(
icon: new Icon(FontAwesomeIcons.download,color: Colors.white),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DownloadView()),
);
},
),
IconButton(
icon: new Icon(FontAwesomeIcons.chevronCircleLeft,color: Colors.white),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainWindow()),
);
},
),]
),
body: Container(
padding: EdgeInsets.symmetric(vertical: 16),
alignment: Alignment.center,
child: new BlocBuilder<SettingsBloc, …Run Code Online (Sandbox Code Playgroud) 首先,我确实知道 BLoC 应该如何工作,它背后的想法以及我知道构造函数BlocProvider()和BlocProvider.value()构造函数之间的区别。
为简单起见,我的应用程序有 3 个页面,其中包含一个像这样的小部件树:
App()=> LoginPage()=> HomePage()=>UserTokensPage()
我希望我LoginPage()可以访问,UserBloc因为我需要登录用户等。为此,我将LoginPage()构建器包装在App()小部件中,如下所示:
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
home: BlocProvider<UserBloc>(
create: (context) => UserBloc(UserRepository()),
child: LoginPage(),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这显然工作得很好。然后,如果用户成功登录,他将被导航到HomePage。现在,我需要访问我的两个不同的块,HomePage所以我用来进一步MultiBlocProvider传递现有的UserBloc并创建一个名为DataBloc. 我这样做:
@override
Widget build(BuildContext context) {
return BlocListener<UserBloc, UserState>(
listener: …Run Code Online (Sandbox Code Playgroud) 没有找到BlocSelector的使用示例。有人知道它的真正用途吗?
我正在使用 flutter_bloc,我想知道我应该使用哪种方法,这两种方法有什么区别?:我读到第一个带有(值)的块不会自动关闭,但实际上我不明白什么是什么意思?
BlocProvider<LoginBloc>.value(
value: (LoginBloc(LoginInitialState(), AuthRepository())),
),
BlocProvider<ProfileBloc>(
create: (context) => ProfileBloc(ProfileInitialState(), AuthRepository()),
),
Run Code Online (Sandbox Code Playgroud) 我的目标是创建一个应用程序,用户可以在其中选择他喜欢的主题。我使用共享首选项保存用户的选择,以便我可以在下次应用程序启动时加载它。用户可以选择: - 深色模式(独立于操作系统设置) - 浅色模式(独立于操作系统设置) - 系统(根据操作系统设置在深色模式和浅色模式之间进行更改) 在 BLoC 的帮助下,我几乎达到了我想要的。但问题是我需要在 Bloc 事件中传递亮度。为了获得系统(操作系统)亮度,我需要利用
MediaQuery.of(context).platformBrightness
Run Code Online (Sandbox Code Playgroud)
但 Bloc 在 MaterialApp 之前启动,因此 MediaQuery 不可用。当然,我可以稍后传递亮度(从 MaterialApp 的子小部件),但随后(例如,如果用户激活了黑暗模式)它会从亮到暗,但对用户来说可见的时间很短(因为在 InitialState 内)我在灯光模式下通过了)。
class MyApp extends StatelessWidget {
final RecipeRepository recipeRepository;
MyApp({Key key, @required this.recipeRepository})
: assert(recipeRepository != null),
super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<ThemeBloc>(create: (context) =>
ThemeBloc(),),
],
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state){
return MaterialApp(
theme: state.themeData,
title: 'Flutter Weather',
localizationsDelegates: [
FlutterI18nDelegate(fallbackFile: 'en',),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [
const Locale("en"), …Run Code Online (Sandbox Code Playgroud) 在我的 flutter 应用程序中,我使用flutter_bloc进行状态管理。
有bloc问题的使用repository. 订阅repositorywebsocket,并将新数据添加到流中。
问题:我的块监听流:
InvestmentClubBloc({
required this.investmentClubRepository
}) : super(InvestmentClubLoading()) {
onChangeSubscription = investmentClubRepository.onNewMessage.listen(
(event) {
emit(NewMessageState(event); // <-- The member "emit" can only be used within 'package:bloc/src/bloc.dart' or in test
},
);
}
Run Code Online (Sandbox Code Playgroud)
问题是emit不起作用(我收到警告“成员“emit”只能在 'package:bloc/src/bloc.dart' 或测试中使用”)
bloc 如何监听流并根据流事件发出新状态?
Widget build(BuildContext context) {
final appBar = AppBar(
title: Text(
"Romantic Comedy",
),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: MaterialSearch(_pagingController.itemList));
},
),
],
);
return Scaffold(
backgroundColor: Colors.black,
appBar: appBar,
body: BlocConsumer<MovieBloc, MovieState>(
listener: (context, movieState) {
},
builder: (context, movieState) {
if (movieState is MovieSuccessState) {
movieBloc.movies.addAll(movieState.movies);
}
return GridView.builder(
controller: _scrollController
..addListener(() {
if (_scrollController.offset ==
_scrollController.position.maxScrollExtent) {
context.bloc<MovieBloc>()
..isFetching = true
..add(Fetch());
}
}),
padding: EdgeInsets.only(left: 12.0, right: 12.0),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( …Run Code Online (Sandbox Code Playgroud) BlocProvider我想对和进行一些澄清RepositoryProvider。从官方 API 参考来看,它们提供相同的功能 - 它们向其后代小部件提供对象的实例。
然而,从名称来看,我想BlocProvider应该仅用于Bloc对象,以及RepositoryProvider其他一切。它是否正确?
示例状态:
abstract class ExampleState extends Equatable {
const ExampleState();
}
class LoadingState extends ExampleState {
//
}
class LoadedState extends ExampleState {
//
}
class FailedState extends ExampleState {
//
}
Run Code Online (Sandbox Code Playgroud)
示例_事件:
abstract class ExampleEvent extends Equatable {
//
}
class SubscribeEvent extends ExampleEvent {
//
}
class UnsubscribeEvent extends ExampleEvent {
//
}
class FetchEvent extends ExampleEvent {
//
}
Run Code Online (Sandbox Code Playgroud)
示例_块:
class ExampleBloc extends Bloc<ExampleEvent, ExampleState> {
@override
ExampleState get initialState => LoadingState();
@override
Stream<ExampleState> mapEventToState(
ExampleEvent event,
) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Bloc 库,并注意到在产生新状态后我的TextFormField初始值没有改变。
我的应用程序比这更复杂,但我做了一个最小的例子。还跟踪推送事件后状态的变化。
Bloc 应该正确重建整个小部件。我错过了什么吗?
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'dart:developer' as developer;
void main() {
runApp(MyApp());
}
enum Event { first }
class ExampleBloc extends Bloc<Event, int> {
ExampleBloc() : super(0);
@override
Stream<int> mapEventToState(Event event) async* {
yield state + 1;
}
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocProvider(
create: (_) => ExampleBloc(),
child: Builder(
builder: (contex) => SafeArea(
child: …Run Code Online (Sandbox Code Playgroud)