如何在Bloc中更改状态?

huangapple go评论67阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_bloc/flutter_bloc.dart&#39;;
import &#39;package:helloapp/profile/bloc/popup/popup_bloc.dart&#39;;

class TestScreen extends StatelessWidget {
  const TestScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) =&gt; PopupBloc(),
      child: BlocBuilder&lt;PopupBloc, PopupState&gt;(
        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(&#39;123&#39;);
        },
      ),
    );
  }
}


popup_bloc.dart
Looking for help from everyone

import &#39;package:bloc/bloc.dart&#39;;
import &#39;package:equatable/equatable.dart&#39;;

part &#39;popup_event.dart&#39;;
part &#39;popup_state.dart&#39;;

class PopupBloc extends Bloc&lt;PopupEvent, PopupState&gt; {
  PopupBloc() : super(PopupLoadingState()) {
    on&lt;PopupEvent&gt;((event, emit) {
      emit(PopupLoadingState());
      emit(PopupLoadedState(avatar: &#39;123&#39;, name: &#39;name&#39;, phone: &#39;phone&#39;));
    });
  }
}


popup_event.dart

part of &#39;popup_bloc.dart&#39;;

abstract class PopupEvent extends Equatable {
  const PopupEvent();

  @override
  List&lt;Object?&gt; get props =&gt; [];
}

class PopupLoadingEvent extends PopupEvent{}

class PopupLoadedEvent extends PopupEvent{}

class PopupErrorEvent extends PopupEvent{}

popup_state.dart
Looking for help from everyone

part of &#39;popup_bloc.dart&#39;;

abstract class PopupState extends Equatable {
  const PopupState();
}

class PopupLoadingState extends PopupState {
  @override
  // TODO: implement props
  List&lt;Object?&gt; get props =&gt; [];

}

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&lt;Object?&gt; get props =&gt; [avatar, name, phone];

}

class PopupErrorState extends PopupState {
  @override
  // TODO: implement props
  List&lt;Object?&gt; get props =&gt; [];

}

答案1

得分: 3

尝试这个

return BlocProvider(
  create: (_) => PopupBloc()..add(PopupEvent.PopupLoadingEvent())..add(PopupEvent.PopupLoadedEvent()),
  ...
);
英文:

try this

return BlocProvider(
          create: (_) =&gt; 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&lt;PopupEvent, PopupState&gt; {
  PopupBloc() : super(PopupLoadingState()) {
    on&lt;PopupLoadingEvent&gt;((event, emit) async {
      emit(PopupLoadingState());
      /// ... do some async operation and finally emit a loaded or error state
      emit(PopupLoadedState(avatar: &#39;123&#39;, name: &#39;name&#39;, phone: &#39;phone&#39;));
    });
  }
}

And trigger this on the creation of the bloc

return BlocProvider(
      create: (_) =&gt; PopupBloc()..add(PopupLoadingEvent()),
     ...
);

huangapple
  • 本文由 发表于 2023年1月6日 12:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定