首页 > 解决方案 > 在我的 Flutter 应用程序中使用从 Firebase 获取和显示数据时遇到问题

问题描述

这是我的 Firebase 数据库:这是 Firebase 数据库

我想用 uid 获取昵称的值。这是我的代码

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class Home extends StatefulWidget {

  final dynamic userUid;
  Home({this.userUid});

  final CollectionReference userNickname  = Firestore.instance.collection('NickNames of Users');

  void getData() {
  userNickname.document(userUid).get();

}

  @override
 _HomeState createState() => _HomeState();
}



class _HomeState extends State<Home> {



  final int delayedAmount = 500;
  //final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {

    return Container(
      child: Scaffold(
        backgroundColor: Colors.greenAccent[400],

  body:  SingleChildScrollView(
    physics: ScrollPhysics(),
      child: Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.fromLTRB( 0 , 50, 0, 0),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.topLeft,
                              child: DelayedAnimation(
               child:  
                Text(
                    'Welcome Ayomikun:',//This i where the firebase nickName code will go
                    style: TextStyle(
                      fontFamily: 'Comic',
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                      fontSize: 30
                    ),
                    ),

这是我用来与 Firebase 数据库通信的方式:

import 'package:cloud_firestore/cloud_firestore.dart';


class DataBaseServices{

final uid;
DataBaseServices({this.uid});

//To create a reference to the firebase
final CollectionReference userNickname  = Firestore.instance.collection('NickNames of Users');

//Now making a function that creates a new document automatically for each user and puts in there nickname..

Future createDocumentWithUid(dynamic nickname) async{
         return await userNickname.document(uid).setData({
               'MyNickName' : nickname,
         });
   }
 }

请在有状态小部件中将昵称的值作为文本获取时需要帮助。谢谢你 ................................................ ..................................................... ..................................................... ..................................................... ..................................................... …………

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


推荐阅读