Like Button Sample Project


Today I am going to build a Like button using flutter and fire-store. Actually this is not a button. It displays how to code for "Like Button". Here I am using authenticate users and their user id.




Here is the Sample Code for this project.


class PublishPost extends StatefulWidget{

  @override
  _PublishPost createState() => new _PublishPost();
}
class _PublishPost extends State<PublishPost> {
  bool isPostLiked = false;
    @override
    Widget _buildListItem (BuildContext context, DocumentSnapshot document) {

      List<String> users;


      Widget child;

      if (document['likedby'].contains('4')) {

        child = Text('Liked');
      } else {
        child = Text('Like');
      }
      return ListTile(
        title: Row(
          children: [
            Expanded(
              child: Text(
                document['content'],
                style: Theme
                    .of(context)
                    .textTheme
                    .headline,),

            ),

            Container(

              child: child,

            ),



          ],

        ),

        onTap: () {

          Firestore.instance.runTransaction((transaction) async {

            DocumentSnapshot freshSnap = await transaction.get(document.reference);
            await transaction.update(freshSnap.reference, {
              'vote': freshSnap['vote'] + 1,
                    });
            users = ["4"]; // userid
            await transaction.update(freshSnap.reference, {
              'likedby': FieldValue.arrayUnion(users),

            });

          });

        },

      );

    }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('Texting Post'),
      ),
      body: StreamBuilder(
          stream: Firestore.instance.collection('/posts').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('Loading...');
            return ListView.builder(
              itemExtent: 80.0,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListItem(context, snapshot.data.documents[index]),

            );

          }),

    );

  }



  }




Firebase DB:

Table: posts

Columns:

Column Name
Data Type
postid
String
content
String
likedby
Array

Here is the github link : https://github.com/SayalaGalvin/LikeButton

Happy Coding !😉

Comments

Popular posts from this blog

C# Analog Clock

Basic Prolog (List)

SOLID Principles