firebase_realtime_demo.dart

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.5 kB
25
Indexable
Never
import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:firebase_database/firebase_database.dart';

class FirebaseRealtimeDemoScreen extends StatefulWidget {
  const FirebaseRealtimeDemoScreen({Key key}) : super(key: key);

  @override
  State<FirebaseRealtimeDemoScreen> createState() =>
      _FirebaseRealtimeDemoScreenState();
}

class _FirebaseRealtimeDemoScreenState
    extends State<FirebaseRealtimeDemoScreen> {
  final databaseReference = FirebaseDatabase.instance.ref();

  String name;
  String deskripsi;
  String id;
  String result;

  TextEditingController nameController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();
  TextEditingController idController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    readData();
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Realtime Database Demo'),
      ),
      body: Center(
          child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: TextField(
                controller: nameController,
                keyboardType: TextInputType.text,
                decoration: InputDecoration(
                  labelText: 'Nama Lengkap',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                ),
                onChanged: (value) {
                  //
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: TextField(
                controller: descriptionController,
                keyboardType: TextInputType.text,
                decoration: InputDecoration(
                  labelText: 'Deskripsi Team',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                ),
                onChanged: (value) {
                  //
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: TextField(
                controller: idController,
                keyboardType: TextInputType.text,
                decoration: InputDecoration(
                  labelText: 'ID',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                ),
                onChanged: (value) {
                  //
                },
              ),
            ),
            ElevatedButton(
              child: const Text('Create Data'),
              //color: Colors.redAccent,
              onPressed: () {
                createData();
              },
              //shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
            ),
            const SizedBox(
              height: 8,
            ),
            ElevatedButton(
              child: const Text('Read/View Data'),
              //color: Colors.redAccent,

              onPressed: () {
                readData();
              },
              //shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
            ),
            const SizedBox(
              height: 8,
            ),
            ElevatedButton(
              child: const Text('Update Data'),
              //color: Colors.redAccent,

              onPressed: () {
                updateData();
              },
              //shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
            ),
            const SizedBox(
              height: 8,
            ),
            ElevatedButton(
              child: const Text('Delete Data'),
              //color: Colors.redAccent,

              onPressed: () {
                deleteData();
              },
              //shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
            ),
          ],
        ),
      )), //center
    );
  }

  void createData() {
    name = nameController.text;
    deskripsi = descriptionController.text;
    id = idController.text;
    databaseReference.child(id).set({'name': name, 'description': deskripsi});

/*    databaseReference
        .child("flutterDevsTeam1")
        .set({'name': 'Deepak Nishad', 'description': 'Team Lead'});
    databaseReference.child("flutterDevsTeam2").set(
        {'name': 'Yashwant Kumar', 'description': 'Senior Software Engineer'});
    databaseReference
        .child("flutterDevsTeam3")
        .set({'name': 'Akshay', 'description': 'Software Engineer'});
    databaseReference
        .child("flutterDevsTeam4")
        .set({'name': 'Aditya', 'description': 'Software Engineer'});
    databaseReference
        .child("flutterDevsTeam5")
        .set({'name': 'Shaiq', 'description': 'Associate Software Engineer'});
    databaseReference
        .child("flutterDevsTeam6")
        .set({'name': 'Mohit', 'description': 'Associate Software Engineer'});
    databaseReference
        .child("flutterDevsTeam7")
        .set({'name': 'Naveen', 'description': 'Associate Software Engineer'});*/
  }

  void readData() {
    //databaseReference.once().then((DataSnapshot snapshot) {
    //print('Data : ${snapshot.value}');
    //});
  }

  void updateData() {
    databaseReference.child('flutterDevsTeam1').update({'description': 'CEO'});
    databaseReference
        .child('flutterDevsTeam2')
        .update({'description': 'Team Lead'});
    databaseReference
        .child('flutterDevsTeam3')
        .update({'description': 'Senior Software Engineer'});
  }

  void deleteData() {
    id = idController.text;
    databaseReference.child(id).remove();

    // databaseReference.child('flutterDevsTeam1').remove();
    // databaseReference.child('flutterDevsTeam2').remove();
    // databaseReference.child('flutterDevsTeam3').remove();
  }
}