如何使用flutter从Firebase实时检索数据

问题描述 投票:0回答:1

我下面有一个屏幕

enter image description here

当我单击配置文件按钮时,我想从我的Firebase实时数据库中检索当前配置文件的所有数据,并显示在下一页(如下图)。

enter image description here

这些是我想在上一页中显示的数据。enter image description here

这是我的个人资料编辑屏幕设计代码。

Widget _NameTextField(String _name) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          'Full Name',
          style: kLabelStyle,
        ),
        SizedBox(height: 10.0),
        Container(
          alignment: Alignment.centerLeft,
          decoration: kBoxDecorationStyle,
          height: 60.0,
          child: TextFormField(
            autovalidate: false,
            style: TextStyle(
              color: Colors.white,
              fontFamily: 'OpenSans',
            ),
            decoration: InputDecoration(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(top: 14.0),
              prefixIcon: Icon(
                Icons.perm_identity,
                color: Colors.white,
              ),
              hintText: 'Enter your Full Name',
              hintStyle: kHintTextStyle,
            ),
            initialValue: _name,
            validator: (String value) {
              if (value.isEmpty) {
                return 'Name is Required.';
              }
              if (value.length < 3) {
                return 'Name must be more than 2 charater';
              }
              return null;
            },
            onSaved: (String value) {
              return _name = value;
            },
          ),
        ),
      ],
    );
  }

  Widget _NICTextField() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          'NIC No:',
          style: kLabelStyle,
        ),
        SizedBox(height: 10.0),
        Container(
          alignment: Alignment.centerLeft,
          decoration: kBoxDecorationStyle,
          height: 60.0,
          child: TextFormField(
            keyboardType: TextInputType.text,
            autovalidate: false,
            style: TextStyle(
              color: Colors.white,
              fontFamily: 'OpenSans',
            ),
            decoration: InputDecoration(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(top: 14.0),
              prefixIcon: Icon(
                Icons.assignment_ind,
                color: Colors.white,
              ),
              hintText: 'Enter your NIC Number',
              hintStyle: kHintTextStyle,
            ),
            validator: (String value) {
              if (value.isEmpty) {
                return 'NIC is Required.';
              }
              return null;
            },
            onSaved: (String value) {
              return _nic = value;
            },
          ),
        ),
      ],
    );
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        leading: IconButton(
          icon:Icon(Icons.close),
          onPressed:() => Navigator.of(context).pop(null),
        ),
        centerTitle: true ,
        title: Text('Edit Profile',
          style: TextStyle(
            color: Colors.white,
            fontSize: 20.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: ()async{
              print('Profile Updated!!!');
            },
            child: Container(
              padding: EdgeInsets.only(top: 17, right: 25),
              child: Text(
                'SAVE',
                style: TextStyle(fontSize: 18),
              ),
            ),
          ),
        ],
      ),
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Stack(
            children: <Widget>[
              Container(
                height: double.infinity,
                width: double.infinity,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color(0xFF73AEF5),
                      Color(0xFF61A4F1),
                      Color(0xFF478DE0),
                      Color(0xFF398AE5),
                    ],
                    stops: [0.1, 0.4, 0.7, 0.9],
                  ),
                ),
              ),
              Container(
                height: double.infinity,
                child: SingleChildScrollView(
                  physics: AlwaysScrollableScrollPhysics(),
                  padding: EdgeInsets.symmetric(
                    horizontal: 35.0,
                    vertical: 5.0,
                  ),
                  child: Form(
                    key: formKey,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(height: 15.0),
                        imageAvatar,
                        _NameTextField(_name),
                        SizedBox(
                          height: 15.0,
                        ),
                        _NICTextField(),
                        SizedBox(
                          height: 15.0,
                        ),
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

我对flutter和firebase数据库不熟悉,任何人都可以帮助我。这是给我的Uni项目的。谢谢

firebase flutter firebase-realtime-database dart
1个回答
0
投票

要检索数据,请尝试以下操作:

db = FirebaseDatabase.instance.reference().child("Users");
db.once().then((DataSnapshot snapshot){
  Map<dynamic, dynamic> values = snapshot.value;
     values.forEach((key,values) {
      print(values["Email"]);
    });
 });

向节点Users添加引用,然后迭代并检索数据。要检索一个特定用户的数据,那么您需要使用查询,因为您没有使用firebase auth:

db.orderByChild("Full Name").equalTo("Dasun Shanaka").once()
© www.soinside.com 2019 - 2024. All rights reserved.