Flutter Firebase PDF
Flutter Firebase PDF
- A cloud-hosted database that helps us to store and sync data with NoSQL
database in realtime to every connected client platforms like Android, iOS,
and Web.
- Firebase store all the data in JSON format and any changes in data reflects
immediately by performing a sync operation across all the platforms.
- Allows us to build a flexible realtime app easily with minimal efforts.
class Mahasiswa {
String _id;
String _nama;
Mahasiswa(this._id,this._nama);
Mahasiswa.fromSnapshot(DataSnapshot snapshot) {
_id = snapshot.key;
_nama = snapshot.value['nama'];
}
}
DatabaseCrud.internal();
//A factory interface that also reports the type of the created objects.
//class contstructor
factory DatabaseCrud() {
return _instance;
}
//initState
b. Inistate
void initState() {
//getError
//getCounter
//getAllUser
//dispose
c. Get Function
//getError
DatabaseError getError() {
return error;
}
//getCounter
int getCounter() {
return _counter;
}
//getAllUser
DatabaseReference getUser() {
return _userRef;
}
//dispose
void dispose() {
_messagesSubscription.cancel();
_counterSubscription.cancel();
}
d. deleteUser
void deleteUser(Mahasiswa user) async {
await _userRef.child(user.id).remove().then((_) {
print('Transaction committed.');
});
}
e. addUser
//addUser
addUser(Mahasiswa user) async {
final TransactionResult transactionResult =
await _counterRef.runTransaction((MutableData mutableData) async {
mutableData.value = (mutableData.value ?? 0) + 1;
return mutableData;
});
if (transactionResult.committed) {
_userRef.push().set(<String, String>{
"nama": "" + user.nama
}).then((_) {
print('Transaction committed.');
});
} else {
print('Transaction not committed.');
if (transactionResult.error != null) {
print(transactionResult.error.message);
}
}
}
f. updateUser
//updateUser
void updateUser(Mahasiswa user) async {
await _userRef.child(user.id).update({
"nama": "" + user.nama
}).then((_) {
print('Transaction committed.');
});
}
//inistate
//dispose
//showUser
//getShortName
//deleteUser
//showEditWidget
@override
Widget build(BuildContext context) {
// TODO: implement build
return null;
}
}
b. Widget build
@override
Widget build(BuildContext context) {
// it will show title of screen
Widget _buildTitle(BuildContext context) {
return new InkWell(
child: new Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'Data User',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
);
}
//It will show new user icon
List<Widget> _buildActions() {
return <Widget>[
new IconButton(
icon: const Icon(
Icons.group_add,
color: Colors.white,
), // display pop for new entry
onPressed: () => null,//showEditWidget(null, false)
),
];
}
// Firebase predefile list widget. It will get user info from firebase
database
body: new FirebaseAnimatedList(
key: new ValueKey<bool>(_anchorToBottom),
query: databaseUtil.getUser(),
reverse: _anchorToBottom,
sort: _anchorToBottom
? (DataSnapshot a, DataSnapshot b) => b.key.compareTo(a.key)
: null,
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation<double> animation, int index) {
return new SizeTransition(
sizeFactor: animation,
child: showUser(snapshot),
);
},
),
);
}
c. showUser
//It will display a item in the list of users.
Widget showUser(DataSnapshot res) {
Mahasiswa user = Mahasiswa.fromSnapshot(res);
return item;
}
d. getShortName
//Get first letter from the name of user
String getShortName(Mahasiswa user) {
String shortName = "";
if (!user.nama.isEmpty) {
shortName = user.nama.substring(0, 1);
}
return shortName;
}
e. deleteUser
//Delete a entry from the Firebase console.
deleteUser(Mahasiswa user) {
setState(() {
databaseUtil.deleteUser(user);
});
}
//call delete function =
new IconButton(
icon: const Icon(Icons.delete_forever,
color: const Color(0xFF167F67)),
onPressed: () => deleteUser(user),
),
f. showEditWidget
//showEditWidget
showEditWidget(Mahasiswa user, bool isEdit) {
showDialog(
context: context,
builder: (BuildContext context) =>
new CrudDialog().buildAboutDialog(context, this, isEdit, user),
);
}
g. addUser
@override // Call util method for add user information
void addUser(Mahasiswa user) {
setState(() {
databaseUtil.addUser(user);
});
}
h. updateUser
i.
@override // call util method for update old data.
void update(Mahasiswa user) {
setState(() {
databaseUtil.updateUser(user);
});
}
j. dispose()
@override
void dispose() {
super.dispose();
databaseUtil.dispose();
}
k. iniState
@override
void initState() {
super.initState();
databaseUtil = new DatabaseCrud();
databaseUtil.initState();
}
class CrudDialog {
final teName = TextEditingController();
Mahasiswa user;
//buildAboutDialog
//linkStyle
//buildAboutDialog
//getTextField
//getAppBorderButton
//getData
//onTap
}
//Call back of user dashboad
abstract class AddUserCallback {
void addUser(Mahasiswa user);
b. Linkstyle
//linkStyle
static const TextStyle linkStyle = const TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
);
c. buildAboutDialog
//buildAboutDialog
Widget buildAboutDialog(BuildContext context,
AddUserCallback _myHomePageState, bool isEdit, Mahasiswa user) {
if (user != null) {
this.user = user;
teName.text = user.nama;
}
d. getTextField
//getTextField
Widget getTextField(
String inputBoxName, TextEditingController inputBoxController) {
var loginBtn = new Padding(
padding: const EdgeInsets.all(5.0),
child: new TextFormField(
controller: inputBoxController,
decoration: new InputDecoration(
hintText: inputBoxName,
),
),
);
return loginBtn;
}
e. getAppBorderButton
//getAppBorderButton
Widget getAppBorderButton(String buttonLabel, EdgeInsets margin) {
var loginBtn = new Container(
margin: margin,
padding: EdgeInsets.all(8.0),
alignment: FractionalOffset.center,
decoration: new BoxDecoration(
border: Border.all(color: const Color(0xFF28324E)),
borderRadius: new BorderRadius.all(const Radius.circular(6.0)),
),
child: new Text(
buttonLabel,
style: new TextStyle(
color: const Color(0xFF28324E),
fontSize: 20.0,
fontWeight: FontWeight.w300,
letterSpacing: 0.3,
),
),
);
return loginBtn;
}
f. getData
//getData
Mahasiswa getData(bool isEdit) {
return new Mahasiswa(isEdit ? user.id : "", teName.text);
}
g. onTap
//onTap
onTap(bool isEdit, AddUserCallback _myHomePageState, BuildContext context) {
if (isEdit) {
_myHomePageState.update(getData(isEdit));
} else {
_myHomePageState.addUser(getData(isEdit));
}
Navigator.of(context).pop();