意外的输出当使用get.put in flutter

vi4fp9gy  于 2023-02-16  发布在  Flutter
关注(0)|答案(2)|浏览(118)

为了使我的观点清楚,我创建了这个小代码。
看起来我在这里没有理解byref和byval的概念。
以及什么是真正有效的。
我已经评论了那些让我困惑的台词
获取控制器

class UserController extends GetxController  {
  List<UserModel> _userlist = [];

  UserModel? currentuser;

  UserController() {
     _userlist.add(UserModel(username: 'temp', password: 't123'));
  }

  List<UserModel> get getusers {
    return _userlist;
  }
}

这是主要的

void main()
{
  final uc1=Get.put(UserController());
  UserModel user1=UserModel(username: 'abc', password: 'a123');
  uc1.currentuser=user1;

  //showing print abc
  print(uc1.currentuser!.username);

  final uc2=Get.put(UserController());
  UserModel user2=UserModel(username: 'xyz', password: 'x123');
  uc2.currentuser=user2;

  //showing printing result=xyz
  print(uc2.currentuser!.username);

  //showing printing result=xyz, instead of abc why?
  print(uc1.currentuser!.username);

}
ua4mk5z4

ua4mk5z41#

Get.put仅在没有示例(或已有示例被标记为脏示例)的情况下放置一个新示例。您可以通过深入研究代码来查看它。按住Ctrl键并单击put方法(至少在Android Studio中是这样),您可以查看实现。第一步是

S put<S>(S dependency,
          {String? tag,
          bool permanent = false,
          InstanceBuilderCallback<S>? builder}) =>
      GetInstance().put<S>(dependency, tag: tag, permanent: permanent);

那么在put上再做一次会导致

S put<S>(
    S dependency, {
    String? tag,
    bool permanent = false,
    @deprecated InstanceBuilderCallback<S>? builder,
  }) {
    _insert(
        isSingleton: true,
        name: tag,
        permanent: permanent,
        builder: builder ?? (() => dependency));
    return find<S>(tag: tag);
  }

深入研究_insert方法得到:

/// Injects the Instance [S] builder into the `_singleton` HashMap.
  void _insert<S>({
    bool? isSingleton,
    String? name,
    bool permanent = false,
    required InstanceBuilderCallback<S> builder,
    bool fenix = false,
  }) {
    final key = _getKey(S, name);

    if (_singl.containsKey(key)) {
      final dep = _singl[key];
      if (dep != null && dep.isDirty) {
        _singl[key] = _InstanceBuilderFactory<S>(
          isSingleton,
          builder,
          permanent,
          false,
          fenix,
          name,
          lateRemove: dep as _InstanceBuilderFactory<S>,
        );
      }
    } else {
      _singl[key] = _InstanceBuilderFactory<S>(
        isSingleton,
        builder,
        permanent,
        false,
        fenix,
        name,
      );
    }
  }

这里你可以看到如果它已经存在并且isDirty为真,或者它还不存在,那么它只会插入一个新的示例,现在我不完全确定什么时候设置isDirty,但我相信当你在应用程序中改变路线时会发生这种情况。所以第二次调用put时,你实际上是在检索之前已经放在那里的路线。现在,如果你想拥有多个示例,你可以使用Get.create(() => UserController());,后面跟着Get.find

void main()
{
  Get.create(() => UserController());
  UserController uc1 = Get.find();
  UserModel user1=UserModel(username: 'abc', password: 'a123');
  uc1.currentuser=user1;

  //showing print abc
  print(uc1.currentuser!.username);

  UserController uc2 = Get.find();
  UserModel user2=UserModel(username: 'xyz', password: 'x123');
  uc2.currentuser=user2;

  //showing printing result=xyz
  print(uc2.currentuser!.username);

  //showing printing result=abc
  print(uc1.currentuser!.username);

}
dced5bon

dced5bon2#

因为您要使用put方法两次,所以如果您要更新单个示例,请遵循以下代码:

void main()
{
  final uc1=Get.put(UserController());
  UserModel user1=UserModel(username: 'abc', password: 'a123');
  uc1.currentuser=user1;

  //showing print abc
  print(uc1.currentuser!.username);

 UserController uc2 = Get.find();
  UserModel user2=UserModel(username: 'xyz', password: 'x123');
  uc2.currentuser=user2;

  //showing printing result=xyz
  print(uc2.currentuser!.username);

  //showing printing result=abc
  print(uc1.currentuser!.username);

}

相关问题