首页 > 解决方案 > 在 Textformfield 中显示来自 Firebase 的文本?

问题描述

我只想显示保存在云 Firestore 中的用户的用户名。有谁知道如何在云 Firestore 中显示用户名字符串?

@override
Widget build(BuildContext context) {
  lol()async{
    var documents =
    (await Firestore.instance.collection('SerX').get())
    .docs;
  }

  return  Scaffold(
    appBar: AppBar(
        bottom: PreferredSize(
            child: Container(
              color: Colors.blue,
              height: 4.0,
            ),
            preferredSize: Size.fromHeight(0)),
        backgroundColor: Colors.black,
        leading: IconButton(icon: Icon(Icons.arrow_back ,color: Colors.blue,),onPressed: (){
          Navigator.pushReplacement(context,
              MaterialPageRoute(builder: (context) => HomeScreen()));
        },),
        title: Text('Profile', style: TextStyle(
          color:Colors.white,
          fontSize: 25,
          fontWeight: FontWeight.bold,
          fontFamily: 'Orbitron',
        ),
          textAlign: TextAlign.center,
        ),
      ),
    body: Container(
      color: Colors.black,
      child: ListView(
        children: [
          Stack(
            children: [
              Container(
                margin: EdgeInsets.fromLTRB(140,100,0,0),
                width: 130,
                height: 130,
                decoration: BoxDecoration(
                    border: Border.all(
                        width: 3,
                        color: Colors.blue,
                    ),
                    boxShadow: [BoxShadow(
                      spreadRadius: 2, blurRadius: 10,
                      color: Colors.white.withOpacity(0.1),
                      offset: Offset(0,10),
                    ),
                    ],
                    shape: BoxShape.circle,
                    image: DecorationImage(
                      fit: BoxFit.cover,
                      image: (_image != null) ? FileImage(
                          _image)
                          : NetworkImage("https://www.senertec.de/wp-content/uploads/2020/04/blank-profile-picture-973460_1280.png"),
                    )
                ),
              ),
              Positioned(
                bottom: 0,
                right: 150,
                child: Container(
                  height: 40,
                  width: 40,
                  decoration: BoxDecoration(
                    border: Border.all(width: 2, color: Colors.blue),
                    shape: BoxShape.circle,
                    color: Colors.white,
                  ),
                  child: IconButton(icon: Icon(Icons.edit), color: Colors.blue, onPressed: (){
                    getImage(context);
                  },),
                ),
              )
            ],
          ),
          SizedBox(height: 40),
          TextFormField(
            style: TextStyle(
              color: Colors.white,
              fontSize: 15,
              fontWeight: FontWeight.bold,
              fontFamily: 'Orbitron',
              ),
            decoration: InputDecoration(
              focusedBorder: OutlineInputBorder(
                borderSide: const BorderSide(color: Colors.blue, width: 1.0),
                borderRadius: BorderRadius.circular(25.0),
              ),
              enabledBorder: OutlineInputBorder(
              borderSide: const BorderSide(color: Colors.white, width: 1.0),
            borderRadius: BorderRadius.circular(25.0),
            ),
              icon: Icon(Icons.edit, color: Colors.blue,),
              contentPadding: EdgeInsets.only(bottom: 3),
              labelText: "Username",
              labelStyle: TextStyle(color:  Colors.white),
              floatingLabelBehavior: FloatingLabelBehavior.always,
              prefixIcon: Icon(Icons.account_circle, color: Colors.blue,),
      ->      hintText: snapshot.data.data()["Username"],    <-
              hintStyle: TextStyle(
                color: Colors.white,
                fontSize: 15,
                fontWeight: FontWeight.bold,
                fontFamily: 'Orbitron',
              ),
            ),
          ),

标签: firebasefluttertext

解决方案


如果您使用的是 firebase 用户身份验证,那么在 firebaseUser 的帮助下,您可以轻松访问用户名。否则,如果您使用的是像 firestore 这样的数据库,那么您从中获得的数据是 JSON 格式(如 Maps)。

字符串用户名 = firebaseData['userName'];


推荐阅读