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

时间:2020-05-23 16:21:23

标签: firebase flutter firebase-realtime-database dart

我在下面看到一个屏幕

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项目的。 谢谢

1 个答案:

答案 0 :(得分:2)

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

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