flutter firebase save data code example

Example 1: flutter firebase database

dependencies:  flutter:    sdk: flutter  firebase_database: 1.0.3

Example 2: how to add data in firebase in flutter

StreamBuilder(
    stream: _firebaseRef.onValue,
    builder: (context, snap) {

        if (snap.hasData && !snap.hasError && snap.data.snapshot.value != null) {

            Map data = snap.data.snapshot.value;
            List item = [];

            data.forEach((index, data) => item.add({"key": index, ...data}));

            return ListView.builder(
                    itemCount: item.length,
                    itemBuilder: (context, index) {
                    return ListTile(
                        title: Text(item[index]['message']),
                        trailing: Text(DateFormat("hh:mm:ss")
                            .format(DateTime.fromMicrosecondsSinceEpoch(
                                item[index]['timestamp'] * 1000))
                            .toString()),
                        onTap: () =>
                            updateTimeStamp(item[index]['key']),
                        onLongPress: () =>
                            deleteMessage(item[index]['key']),
                    );
                    },
                );
        }
        else
            return Text("No data");
    },
)

Example 3: flutter firebase database

void createRecord(){  databaseReference.child("1").set({    'title': 'Mastering EJB',    'description': 'Programming Guide for J2EE'  });  databaseReference.child("2").set({    'title': 'Flutter in Action',    'description': 'Complete Programming Guide to learn Flutter'  });}

Tags:

Go Example