This is the BrewTile which makes up a Brew and goes to a BrewList. There is a like iconButton for each brew in the list . How can i get any user to like the brew or unlike the brew..it doesn't have to show the name of the users that liked the Brew..it just needs to show the total number of the likes. And also the user has to be able to unlike if tapped second time.
class Brew {
final String id;
final String name;
final String sugars;
final int strength;
final int likeCount;
Brew({this.id, this.name, this.sugars, this.strength,
this.likeCount});
}
class BrewData {
final String id;
final String name;
final String sugars;
final int strength;
final int likeCount;
BrewData({this.id, this.name, this.sugars, this.strength,
this.likeCount});
factory BrewData.fromDoc(DocumentSnapshot doc) {
return BrewData(
id: doc.documentID,
name: doc['name'],
sugars: doc['sugars'],
strength: doc['strength'],
likeCount: doc['likeCount'],
);
}
}
class BrewTile extends StatefulWidget {
final Brew brew;
BrewTile({ this.brew});
@override
_BrewTileState createState() => _BrewTileState();
}
class _BrewTileState extends State<BrewTile> {
int _likeCount = 0;
bool _isLiked = false;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
leading: CircleAvatar(
radius: 25.0,
backgroundColor: Colors.brown[brew.strength],
backgroundImage:
AssetImage('assets/coffee_icon.png'),
),
title: Text(brew.name),
subtitle: Text('Takes ${brew.sugars} sugar(s)'),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: _isLiked
? Icon(
Icons.favorite,
color: Colors.blue,
)
: Icon(Icons.favorite_border),
iconSize: 30.0,
onPressed: () {
if (_isLiked) {
_likeCount++;
_isLiked = false;
print(_likeCount);
DatabaseService()
.updateLikes(id: widget.brew.id, value:
1);
} else {
print(true);
_likeCount--;
_isLiked = true;
DatabaseService()
.updateLikes(id: widget.brew.id, value:
-1);
print(_likeCount);
}
});
},
),
Padding(
padding: EdgeInsets.symmetric(horizontal:
12.0),
child: Text(
'${_likeCount.toString()} likes',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
),
],
)
],
),
)
],
);
}
}
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
//print(doc.data);
return Brew(
id: doc.documentID ?? '',
name: doc.data['name'] ?? '',
strength: doc.data['strength'] ?? 0,
sugars: doc.data['sugars'] ?? '0',
likeCount: doc.data['likeCount'] ?? 0,);
}).toList();
}
Future<void> updateLikesCount({String id int value}) async {
return await brewCollection
.document(id)
.updateData({'likeCount': FieldValue.increment(value)});
}
Future<void> updateBrewData(String sugars, String name, int strength, int likeCount) async {
return await brewCollection.document(uid).setData({
'sugars': sugars,
'name': name,
'strength': strength,
'likeCount': likeCount,
});
}
Stream<BrewData> get brewData {
return brewCollection.document(uid).snapshots().map(_brewDataFromSnapshot);
}
It gives this error in the console.
flutter: true
flutter: -1
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: type 'MethodChannelFieldValue' is not a subtype of type 'FieldValuePlatform'
#0 FieldValue.increment
package:cloud_firestore/src/field_value.dart:51
#1 DatabaseService.updateLikesCount
package:myblogprofile/services/database.dart:75
#2 _BrewTileState.build.<anonymous closure>.<anonymous closure>
package:myblogprofile/…/home/brew_tile.dart:77
#3 State.setState
package:flutter/…/widgets/framework.dart:1148
#4 _BrewTileState.build.<anonymous closure>
package:myblogprofile/…/home/brew_tile.dart:65
#5 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:706
#6 _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:789
#7 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:182
#8 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:486
#9 BaseTapGestur<…>
Please add below code in onPressed event of IconButton...
setState(() {
if (_isLiked) {
_likeCount = _likeCount--;
_isLiked = false;
} else {
_likeCount = _likeCount++;
_isLiked = true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With