【问题标题】:The argument type 'Future<dynamic>' can't be assigned to the parameter type 'String' Flutter Firestore参数类型“Future<dynamic>”不能分配给参数类型“String”Flutter Firestore
【发布时间】:2021-12-30 21:17:49
【问题描述】:

我有这个问题:

参数类型“Future”不能分配给参数类型“String”

我的代码:

CupertinoButton(
          child: Text("OK"),
          onPressed: () async {
            print("- start customer creation -. ");

            if (_key.currentState!.validate()) {
              await firebase.newCustomerSetup(
                  email: _emailController.text, name: input, companyID: FirebaseController().getCompanyID()); 
              setState(() {});

              //UpdateListViewBuilder
            }
          },
        ),

Future getCompanyID() async{
    var firebaseUser =  FirebaseAuth.instance.currentUser;
    FirebaseFirestore.instance
        .collection("Company")
        .doc(firebaseUser!.uid)
        .get()
        .then((value) {
      return value.data()!["companyID"];
    });

  }

我可以在没有 Future Builder 的情况下获取并粘贴公司 ID 吗?

情况如下:我有一个软件供公司和他们的客户使用。公司可以在软件中创建客户资料(注册他们,客户将获得访问电子邮件),客户将显示在公司方面的 ListView 中。

我的问题是,每个新注册的公司都可以在 ListView 中看到所有客户(也来自其他公司),因为所有客户都将粘贴到一个集合中。我的解决方案是,每家公司都会获得一个 ID,并且在公司注册新客户后,该 ID 也会粘贴到客户集合中。然后只有在 CustomerID == CompanyID 的情况下,客户才会显示在正确的公司资料 (ListView) 中。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    只需在FirebaseController().getCompanyID()之前添加await

    【讨论】:

    • 不是完整的解决方案,但非常有帮助。谢谢! :)
    • @niklasknuf 你找到最终解决方案了吗?如果是这样,您能否将其添加为答案?您也可以查看相关的thread,这可能会有所帮助。
    【解决方案2】:
    CupertinoButton(
              child: Text("OK"),
              onPressed: () async {
                print("- start customer creation home-. ");
    
                String companyID = await firebase.getCompanyID();
                await firebase.newCustomerSetup(
                    email: _emailController.text, name: input, CompanyID:  companyID);
    
                print("- end customer creation home-. ");
              },
            ),
    
    class FirebaseController {
      FirebaseAuth auth = FirebaseAuth.instance;
      FirebaseFirestore base = FirebaseFirestore.instance;
      late FirebaseApp app;
    
      Future<String> getCompanyID() async {
        var firebaseUser = auth.currentUser;
        String companyID = await base
            .collection("Company")
            .doc(firebaseUser!.uid)
            .get()
            .then((snapshot) {
          return snapshot.data()!["companyID"];
        });
        return companyID;
      }
    
    newCustomerSetup(
          {required String email,
          required String name,
          required String companyID}) async {
        String password = generateRandomString(16);
        base.collection("tempUser").add({
          "email": email,
          "name": name,
          "companyID": companyID,
          "password": password,
        });
      }
        }
    

    失败是当前用户(公司登录)在您注册新客户(使用 createUserWithEmailandPassword)时被覆盖。因此,现在该软件在 Firestore 中创建一个文档,Firestore 功能是处理客户注册,设置随机密码,公司 ID 并向客户发送带有密码的邀请电子邮件。然后 Firebase 规则处理只有具有相同 ID 的客户才会显示在公司软件的 ListView 中。

    这很复杂……

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2021-03-23
      • 2023-04-05
      • 2021-08-03
      • 2020-10-25
      • 1970-01-01
      • 2022-11-17
      • 2020-12-27
      • 2021-11-23
      相关资源
      最近更新 更多