How to use UID to acces database in Flutter?

让人想犯罪 __ 提交于 2020-01-04 09:08:07

问题


i am using the following function to retrive the UID:

  FirebaseAuth auth = FirebaseAuth.instance;

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

After that, i would like to use the UID to acces the right database-doc:

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;

But i get the error

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

What is the right way to do it?

FULL CODE

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;

回答1:


You're passing the function without actually awaiting for it to return. You could store the returned value in a variable before calling 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;



回答2:


You need to use stateful widget, like


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());
        });
  }
}




来源:https://stackoverflow.com/questions/54724966/how-to-use-uid-to-acces-database-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!