stlo0309

firebase_flutter_connect_sample

Dec 15th, 2020
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'package:firebase_database/firebase_database.dart';
  3.  
  4. class FirebaseDemoScreen extends StatelessWidget {
  5.  
  6.   final databaseReference = FirebaseDatabase.instance.reference();
  7.  
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     getData();
  11.     return Scaffold(
  12.         appBar: AppBar(
  13.             title: Text('Firebase Connect'),
  14.             ),
  15.         body: Center(
  16.             child: Column(
  17.                 crossAxisAlignment: CrossAxisAlignment.stretch,
  18.                 children: <Widget>[
  19.  
  20.                   RaisedButton(
  21.                       child: Text('Create Record'),
  22.                       onPressed: () {
  23.                         createRecord();
  24.                       },
  25.                   ),
  26.  
  27.                   RaisedButton(
  28.                       child: Text('View Record'),
  29.                       onPressed: () {
  30.                         getData();
  31.                       },
  32.                   ),
  33.                   RaisedButton(
  34.                       child: Text('Udate Record'),
  35.                       onPressed: () {
  36.                         updateData();
  37.                       },
  38.                   ),
  39.                   RaisedButton(
  40.                       child: Text('Delete Record'),
  41.                       onPressed: () {
  42.                         deleteData();
  43.                       },
  44.                   ),
  45.                 ],
  46.             )
  47.         ), //center
  48.     );
  49.   }
  50.  
  51.   void createRecord(){
  52.     databaseReference.child("1").set({
  53.       'title': 'Mastering EJB',
  54.       'description': 'Programming Guide for J2EE'
  55.     });
  56.     databaseReference.child("2").set({
  57.       'title': 'Flutter in Action',
  58.       'description': 'Complete Programming Guide to learn Flutter'
  59.     });
  60.   }
  61.   void getData(){
  62.     databaseReference.once().then((DataSnapshot snapshot) {
  63.       print('Data : ${snapshot.value}');
  64.     });
  65.   }
  66.  
  67.   void updateData(){
  68.     databaseReference.child('1').update({
  69.       'description': 'J2EE complete Reference'
  70.     });
  71.   }
  72.  
  73.   void deleteData(){
  74.     databaseReference.child('1').remove();
  75.   }
  76. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×