繁体   English   中英

在 flutter 中的 TextForm 的列表中添加更多价值

[英]add more value in the List from TextForm in flutter

我有这个 function:

Future<void> userProfile(String name, age, String codes) async {
    String uid = _firebase.currentUser!.uid;
    try {
      Map<String, dynamic> map;
      map = {
        "name": name,
        'age': age,
        'codes' : [codes],
      };
      await _userCollection.doc(uid).update(map);
    } catch (error) {
      print('updateUserName error: $error');
    }
  }

在这一行:

'codes' : [codes],

我需要从 TextForm 向此列表添加一个值,它工作正常,但每次添加时只需替换值,我只需要在列表中添加另一个值而不是替换第一个值?

如果您使用的是 Firebase,我认为有一个 FieldValue 元素可以帮助您。

这是您使用该 FieldValue 的示例代码

Future<void> userProfile(String name, age, String codes) async {
    String uid = _firebase.currentUser!.uid;
    try {
      Map<String, dynamic> map;
      map = {
        "name": name,
        'age': age,
        'codes' : FieldValue.arrayUnion([codes]),
      };
      await _userCollection.doc(uid).update(map);
    } catch (error) {
      print('updateUserName error: $error');
    }
  }

这是该方法的文档:

返回一个 FieldValue,告诉服务器将给定元素与服务器上已经存在的任何数组值联合。

数组中尚不存在的每个指定元素都将添加到末尾。 如果被修改的字段还不是一个数组,它将被一个包含指定元素的数组覆盖。

这应该 append 您的元素到列表中。 我没有测试它,所以我希望它有效。

暂无
暂无

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

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