简体   繁体   English

使用Bloc,RxDart和Flutter创建用户个人资料页面

[英]Create user profile page using Bloc, RxDart and Flutter

My goal is to create an edit profile page in Flutter using the bloc pattern. 我的目标是使用bloc模式在Flutter中创建一个编辑配置文件页面。

I've searched for a better/cleaner way to create a user profile page in Flutter using Bloc, but I can't find anything. 我一直在寻找更好/更干净的方法来使用Bloc在Flutter中创建用户个人资料页面,但是我什么也找不到。

Right now I have to list every field out. 现在,我必须列出每个字段。 Here's an example of 2 fields: 这是2个字段的示例:

final _firstNameController = BehaviorSubject<String>();
final _lastNameController = BehaviorSubject<String>();

Function(String) get firstNameChanged => _firstNameController.sink.add;
Function(String) get lastNameChanged => _lastNameController.sink.add;

Stream<String> get firstNameStream => _firstNameController.stream;
Stream<String> get lastNameStream => _lastNameController.stream;

String get firstName => _firstNameController.value;
String get lastName => _lastNameController.value;


@override
  void dispose() {
_firstNameController?.close();
_lastNameController?.close();
}

There are a lot more fields and I don't want to have all this code if I can avoid it. 还有更多的字段,如果可以避免的话,我不想拥有所有这些代码。

I'd prefer to only have 1 user bloc and update the specific field of that user. 我希望只有1个用户块并更新该用户的特定字段。 I've added the following to a user bloc. 我已将以下内容添加到用户块。

final _userFetcher = BehaviorSubject<User>();

Observable<User> get userStream => _userFetcher.stream;
User get user => _userFetcher.value;
Function(User) get changeUser => _userFetcher.sink.add;

@override
  void dispose() async {
    await _userFetcher.drain();
    _userFetcher.close();
  }

Here's an example of my user model: 这是我的用户模型的示例:

class User {

  final Name name;

  User.fromJson(Map<String, dynamic> json)
      : name = Name.fromJson(json);

  Map<String, dynamic> toJson() => {
    "first": name.first,
    "last": name.last,
  };
}

The issue is I can't figure out how to use a textfield to edit the "fist name" and "last name" fields in my "User" model. 问题是我不知道如何使用文本字段编辑“用户”模型中的“拳头名称”和“姓氏”字段。

Is this possible, am I going about this the wrong way, or should I stick to listing every field out individually? 这可能吗,我是用错误的方式走下去,还是应该坚持逐一列出每个字段?

To individually manage all those streams for each individual fields can be cumbersome. 单独管理每个单独字段的所有这些流可能很麻烦。 I would recommend you to try out this library flutter bloc . 我建议你尝试一下这个库扑集团 It is a really good one that handles the state management pretty well. 这是一个很好的程序,可以很好地处理状态管理。 You just need to define the states and events and then for each event you can generate a state. 您只需要定义状态和事件,然后可以为每个事件生成一个状态。

So for example you want to validate password field as the user is typing. 因此,例如,您想在用户键入时验证密码字段。 You define an Event(eg. PasswordChanged). 您定义一个事件(例如,PasswordChanged)。 This will call a method in the bloc. 这将在块中调用方法。 In which you can write your business logic to check the validation. 您可以在其中编写业务逻辑以检查验证。

After your validation logic you can yield a new State(error, succcess). 在验证逻辑之后,您可以产生一个新的状态(错误,成功)。 This will cause your UI to be rebuilt and then you can update your UI according to your state. 这将导致您的用户界面被重建,然后您可以根据状态更新用户界面。

You should checkout the documentation of this library. 您应该检查该库的文档。 It also has some very good examples there. 那里也有一些很好的例子。

Thank you everyone for your help! 谢谢你们每一个人的帮助!

I ended up being able to edit the nested first and last name fields in my user model I listed above by implementing the following in my textField: 我最终可以通过在textField中实现以下内容来编辑上面列出的用户模型中嵌套的名字和姓氏字段:

onChanged:(text) {
        User thisUser = _bloc.user;
        thisUser.name.last = text;
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM