如何在Flutter中使用UID访问数据库?

时间:2019-02-16 16:03:08

标签: dart flutter firebase-authentication

我正在使用以下函数来检索UID:

  FirebaseAuth auth = FirebaseAuth.instance;

  getUID() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
   return  uid;
  }

之后,我想使用UID访问正确的数据库文档:

return new StreamBuilder(
        stream: Firestore.instance.collection('users').document(getUID()).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          var userDocument = snapshot.data;

但是我得到了错误

Future<dynamic> is not a subtype of type "string"

正确的方法是什么?

完整代码

class Settings extends StatelessWidget{
  FirebaseAuth auth = FirebaseAuth.instance;

  getUID() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
   return  uid;
  }

  static GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Indstillinger"),
      ),
      body: _buildSetting(context),
    );
  }

  Widget _buildSetting(BuildContext context) {
    return new StreamBuilder(
        stream: Firestore.instance.collection('users').document(getUID()).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          var userDocument = snapshot.data;

2 个答案:

答案 0 :(得分:2)

您正在传递的函数实际上没有awaiting才能返回。您可以在调用StreamBuilder之前将返回值存储在变量中。

final documentId = await getUID();
return new StreamBuilder(    
                stream: Firestore.instance.collection('users').document(documentId).snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return new Text("Loading");
                  }
                  var userDocument = snapshot.data;

答案 1 :(得分:1)

您需要使用有状态的小部件,例如


import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class Settings extends StatefulWidget {
  @override
  _SettingsState createState() {
    return _SettingsState();
  }
}

class _SettingsState extends State<Settings> {
  FirebaseUser user;
  String error;

  void setUser(FirebaseUser user) {
    setState(() {
      this.user = user;
      this.error = null;
    });
  }

  void setError(e) {
    setState(() {
      this.user = null;
      this.error = e.toString();
    });
  }

  @override
  void initState() {
    super.initState();
    FirebaseAuth.instance.currentUser().then(setUser).catchError(setError);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Indstillinger"),
      ),
      body: user != null ? _buildSetting(context) : Text("Error: $error"),
    );
  }

  Widget _buildSetting(BuildContext context) {
    return new StreamBuilder(
        stream: Firestore.instance.collection('users').document(user.uid).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          var userDocument = snapshot.data;
          // User userDocument here ..........
          return new Text(userDocument.toString());
        });
  }
}