问题描述
我第一次使用 Flutter 和 Cubit,我想知道这是否是在我的 cas 中检索存储变量的好方法。
登录表单:
submit(BuildContext context) async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final authCubit = context.read<AuthCubit>();
authCubit.login(
email: _data.email!,password: _data.password!,deviceName: _deviceInfos.deviceName,);
}
}
AuthCubit:登录方式:
class AuthCubit extends Cubit<AuthState> {
dynamic user;
Future<void> login({
required String email,required String password,required String deviceName,}) async {
emit(AuthLoading());
// Get the user from the API
this.user = apiResponse['user'];
emit(AuthConnected(user));
}
}
个人资料页面:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),),body: BlocBuilder<AuthCubit,AuthState>(
builder: (context,state) {
final user = context.read<AuthCubit>().user;
return Center(
child: Column(
children: <Widget>[
Text('Hello,' + (user != null ? user['name'] : 'stranger.')),ElevatedButton(
onpressed: () {
context.read<AuthCubit>().logout();
},child: Text('logoout'),],);
},);
}
非常感谢任何建议/建议。谢谢!
解决方法
好的,所以你的例子有什么问题是你根本没有使用状态。 #include <iostream>
#include <SFML/Graphics.hpp>
float period_ms = 5000.f;
using namespace std;
sf::Vector2f transform(float t)
{
float const ellipse_width = 500.f;
float const ellipse_height = 500.f;
float const a = ellipse_width / 2.f;
float const b = ellipse_height / 2.f;
float const pi = 3.141592653589f;
float const tau = 2.f * pi;
float const x = (std::fmodf(t,period_ms) / period_ms) * tau;
return sf::Vector2f(a * std::cos(x),b * std::sin(x));
}
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(1000,1000),"SFML shapes",sf::Style::Default,settings);
window.setFramerateLimit(144);
//white orbitting circle
sf::CircleShape shape(28.f);
shape.setFillColor(sf::Color::Black);
shape.setOutlineColor(sf::Color::Red);
shape.setOutlineThickness(5);
//white orbitting circle
//center red circle
sf::CircleShape shapeTwo(208.f);
shapeTwo.setFillColor(sf::Color::Red);
shapeTwo.setOutlineThickness(5);
shapeTwo.setOutlineColor(sf::Color(250,150,100));
shapeTwo.setPosition(325.00,320.00);
//base red
sf::Clock clock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
float const t = static_cast<float>(clock.getElapsedTime().asMilliseconds());
shape.setPosition(sf::Vector2f(500.f,500.f) + transform(t));
window.clear();
window.draw(shape);
window.draw(shapeTwo);
window.display();
cout << "Orbitting X: " << shape.getPosition().x << ",Y: " << shape.getPosition().y << endl;
}
return 0;
}
是您状态的一部分,我看到您已经在此处存储了它 (User
),因此只需从 BLoC 中删除 emit(AuthConnected(user)
对象:
user
然后,在 UI 内部,您应该从状态对象而不是直接从 BLoC 获取用户:
class AuthCubit extends Cubit<AuthState> {
Future<void> login({
required String email,required String password,required String deviceName,}) async {
emit(AuthLoading());
// Get the user from the API
final user = apiResponse['user'];
emit(AuthConnected(user));
}
}
现在你应该得到一个错误,因为不是所有的状态都包含 Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),),body: BlocBuilder<AuthCubit,AuthState>(
builder: (context,state) {
final user = state.user; // You should get the error here
return Center(
child: Column(
children: <Widget>[
Text('Hello,' + (user != null ? user['name'] : 'stranger.')),ElevatedButton(
onPressed: () {
context.read<AuthCubit>().logout();
},child: Text('Logoout'),],);
},);
}
对象,例如user
缺少这一点。要解决此问题,您应该检查状态类型并相应地呈现 UI:
AuthLoading