我的Flatter应用程序中有一个使用firebase身份验证的用户管理功能.我可以使用firebase_authcreateUserWithEmailAndPassword()功能注册新用户帐户.

return await FirebaseAuth.instance.
    createUserWithEmailAndPassword(email: email, password: password);

The problem is when the registration is successful it automatically authenticates my 100 instance as the new user even though I am already logged in.

我遇到了这样的答案:100,但它是用javascript编写的,并且API稍有不同.

我怎样才能在dart 中做同样的事情呢?

推荐答案

Updated:firebase_core ^0.5.0firebase_auth ^0.18.0+1已经弃用了一些旧类.

下面是firebase_core ^0.5.1firebase_auth ^0.18.2的代码更新.

static Future<UserCredential> register(String email, String password) async {
    FirebaseApp app = await Firebase.initializeApp(
        name: 'Secondary', options: Firebase.app().options);
    try {
        UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(email: email, password: password);
    }
    on FirebaseAuthException catch (e) {
      // Do something with exception. This try/catch is here to make sure 
      // that even if the user creation fails, app.delete() runs, if is not, 
      // next time Firebase.initializeApp() will fail as the previous one was
      // not deleted.
    }
    
    await app.delete();
    return Future.sync(() => userCredential);
}

Original Answer

我使用Firebase身份验证API进行了试验,我当前的工作解决方案是:

// Deprecated as of `firebase_core ^0.5.0` and `firebase_auth ^0.18.0`.
// Use code above instead.

static Future<FirebaseUser> register(String email, String password) async {
    FirebaseApp app = await FirebaseApp.configure(
        name: 'Secondary', options: await FirebaseApp.instance.options);
    return FirebaseAuth.fromApp(app)
        .createUserWithEmailAndPassword(email: email, password: password);
}

从本质上讲,它需要创建一个FirebaseAuth的新实例,因此从createUserWithEmailAndPassword()自动登录不会影响默认实例.

Dart相关问答推荐

如何判断一个数字是否是dart中另一个数字的倍数?

如何使用 dart 将 Uint8list 转换为 List

在Flutter中通过 Function(T) 传递泛型类型

如何使用新的流 API 在 Dart 中注册自定义事件

如何将指针事件发送到堆栈中的低级子级

在`lib`文件夹中使用`src`子文件夹有什么好处吗

执行 `dart2js` 时会生成哪些文件?为什么?

如何在 Dart 单元测试中模拟或验证打印调用?

什么用例有 FirebaseUser 的 reload() 功能?

Flutter - 如何自动启用 AnimatedOpacity?

异常:在Flatter项目上没有Linux桌面项目配置错误

如何在控件边框(border()/阴影(shadow)中添加霓虹灯效果?

Flutter/Dart Uri 没有在 URL 中转义冒号:

如何在 Dart 中替换字符串中间的空格?

比较在 Dart 中创建单例的方法

具有 Maps 的 Null 感知运算符

如何在 Dart 中逐个字符地迭代字符串?

在 DART 中创建泛型类型的实例

如何在 Dart 中使用 Switch Case 语句

你如何在 Dart 中对异常进行单元测试?