英文:
how to change state in Bloc?
问题
I'm learning about Bloc and have a problem
如何从加载状态切换到已加载状态?
我的代码如下:
test.dart
寻求大家的帮助
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:helloapp/profile/bloc/popup/popup_bloc.dart';
class TestScreen extends StatelessWidget {
const TestScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => PopupBloc(),
child: BlocBuilder<PopupBloc, PopupState>(
builder: (context, state) {
if (state is PopupLoadingState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 50),
child: CircularProgressIndicator(),
),
],
);
}
if (state is PopupLoadedState) {
return Text(state.avatar + state.phone + state.name);
}
return Text('123');
},
),
);
}
}
popup_bloc.dart
寻求大家的帮助
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
part 'popup_event.dart';
part 'popup_state.dart';
class PopupBloc extends Bloc<PopupEvent, PopupState> {
PopupBloc() : super(PopupLoadingState()) {
on<PopupEvent>((event, emit) {
emit(PopupLoadingState());
emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
});
}
}
popup_event.dart
part of 'popup_bloc.dart';
abstract class PopupEvent extends Equatable {
const PopupEvent();
@override
List<Object?> get props => [];
}
class PopupLoadingEvent extends PopupEvent {}
class PopupLoadedEvent extends PopupEvent {}
class PopupErrorEvent extends PopupEvent {}
popup_state.dart
寻求大家的帮助
part of 'popup_bloc.dart';
abstract class PopupState extends Equatable {
const PopupState();
}
class PopupLoadingState extends PopupState {
@override
List<Object?> get props => [];
}
class PopupLoadedState extends PopupState {
final String avatar;
final String name;
final String phone;
PopupLoadedState({
required this.avatar,
required this.name,
required this.phone,
});
@override
List<Object?> get props => [avatar, name, phone];
}
class PopupErrorState extends PopupState {
@override
List<Object?> get props => [];
}
这是您要翻译的内容,代码部分已忽略。
英文:
I'm learning about Bloc and have a problem
How to change from loading state to loaded state?
My code here:
test.dart
Looking for help from everyone
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:helloapp/profile/bloc/popup/popup_bloc.dart';
class TestScreen extends StatelessWidget {
const TestScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => PopupBloc(),
child: BlocBuilder<PopupBloc, PopupState>(
builder: (context, state) {
if (state is PopupLoadingState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 50),
child: CircularProgressIndicator()
),
],
);
}
if (state is PopupLoadedState) {
return Text(state.avatar + state.phone + state.name);
}
return Text('123');
},
),
);
}
}
popup_bloc.dart
Looking for help from everyone
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
part 'popup_event.dart';
part 'popup_state.dart';
class PopupBloc extends Bloc<PopupEvent, PopupState> {
PopupBloc() : super(PopupLoadingState()) {
on<PopupEvent>((event, emit) {
emit(PopupLoadingState());
emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
});
}
}
popup_event.dart
part of 'popup_bloc.dart';
abstract class PopupEvent extends Equatable {
const PopupEvent();
@override
List<Object?> get props => [];
}
class PopupLoadingEvent extends PopupEvent{}
class PopupLoadedEvent extends PopupEvent{}
class PopupErrorEvent extends PopupEvent{}
popup_state.dart
Looking for help from everyone
part of 'popup_bloc.dart';
abstract class PopupState extends Equatable {
const PopupState();
}
class PopupLoadingState extends PopupState {
@override
// TODO: implement props
List<Object?> get props => [];
}
class PopupLoadedState extends PopupState {
final String avatar;
final String name;
final String phone;
PopupLoadedState({
required this.avatar,
required this.name,
required this.phone
});
@override
// TODO: implement props
List<Object?> get props => [avatar, name, phone];
}
class PopupErrorState extends PopupState {
@override
// TODO: implement props
List<Object?> get props => [];
}
答案1
得分: 3
尝试这个
return BlocProvider(
create: (_) => PopupBloc()..add(PopupEvent.PopupLoadingEvent())..add(PopupEvent.PopupLoadedEvent()),
...
);
英文:
try this
return BlocProvider(
create: (_) => PopupBloc()..add(PopupEvent.PopupLoadingEvent())..add(PopupEvent.PopupLoadedEvent()),
...
);
答案2
得分: 2
事件处理程序应该注册到特定事件上。
class PopupBloc extends Bloc<PopupEvent, PopupState> {
PopupBloc() : super(PopupLoadingState()) {
on<PopupLoadingEvent>((event, emit) async {
emit(PopupLoadingState());
/// ... 执行一些异步操作,最后发出加载完成或错误状态
emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
});
}
}
并在创建 bloc 时触发它。
return BlocProvider(
create: (_) => PopupBloc()..add(PopupLoadingEvent()),
// 其他代码...
);
英文:
The event handlers should be registered for the specific event
class PopupBloc extends Bloc<PopupEvent, PopupState> {
PopupBloc() : super(PopupLoadingState()) {
on<PopupLoadingEvent>((event, emit) async {
emit(PopupLoadingState());
/// ... do some async operation and finally emit a loaded or error state
emit(PopupLoadedState(avatar: '123', name: 'name', phone: 'phone'));
});
}
}
And trigger this on the creation of the bloc
return BlocProvider(
create: (_) => PopupBloc()..add(PopupLoadingEvent()),
...
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论