0 points

CRUD realtime database Home file


dashboard

Sun Jun 14 2020 07:37:22 GMT+0000 (UTC)

Posted by @pratik.gtm #dart

import 'package:dynamicmathapp/services/auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:dynamicmathapp/models/user.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class Home extends StatelessWidget {
  final AuthService _auth = AuthService();
  final ref = FirebaseDatabase.instance.reference();

  @override
  Widget build(BuildContext context) {
    User user = Provider.of<User>(context);

    return Scaffold(
      backgroundColor: Colors.brown[50],
      appBar: AppBar(
        title: Text('Welcome'),
        backgroundColor: Colors.brown[400],
        elevation: 0.0,
        actions: <Widget>[
          FlatButton.icon(
            icon: Icon(Icons.person),
            label: Text('logout'),
            onPressed: () async {
              await _auth.signOut();
            },
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Container(
            child: Text(
              'ID:' + user.uid + 'email:' + user.email,
            ),
          ),
          RaisedButton(
            child: Text('Create Record'),
            onPressed: () {
              createRecord();
            },
          ),
          RaisedButton(
            child: Text('View Record'),
            onPressed: () {
              getData();
            },
          ),
          RaisedButton(
            child: Text('Update Record'),
            onPressed: () {
              updateData();
            },
          ),
          RaisedButton(
            child: Text('Delete Record'),
            onPressed: () {
              deleteData();
            },
          ),
        ],
      ),
    );
  }

  void createRecord() {
    ref
        .child('videos')
        .child('class_10')
        .child('chapter_1')
        .set([
      {"length": "1:3333", "title": "Introduction 1.1"},
      {"length": "2:30", "title": "Introduction 1.2"}
    ]);
  }

  void getData() {
    ref
        .child('videos')
        .child('class_10')
        .child('chapter_1')
        .once()
        .then((DataSnapshot data) {
      print(data..value);
      print(data.key);
    });
  }

  void updateData() {}

  void deleteData() {}
}
content_copy Copy