How to retrieve data from firebase realtime with flutter

Viewed 8863

I got a screen below

enter image description here

when I click on the profile button I want to retrieve all the data of the current profile from my firebase realtime database and show on the following page (below picture).

enter image description here

These are the data I wanna show on the above page. enter image description here

this is my code for the edit profile screen design.

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,
                        ),
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Im new to flutter and firebase database can anyone help me. This is for my Uni project. Thank you

1 Answers

To retrieve the data try the following:

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

Add a reference to node Users then iterate and retrieve the data. To retrieve the data of one specific user then you need to use a query since you are not using firebase auth :

db.orderByChild("Full Name").equalTo("Dasun Shanaka").once()
Related