0
Personal talk!
I’m trying to create a function changeLikes
where my firebase document (snapshot) has its likes
incremented by Firebase by clicking on ontap
through the FieldValue
. However, I am receiving the following message "the getter data was called on Null".
Detail, if I specify the document the function works, but I do not want to specify the document. It has a list with several. Does anyone have any idea what I might be doing wrong?
import 'package:e_ai_casimiro/models/likes_model.dart';
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'home_screen.dart';
void main() {
runApp(EaiCasimiro());
}
class EaiCasimiro extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModel<LikesModel>(
model: LikesModel(),
child: MaterialApp(
title: "E aí, Casimiro?",
home: HomeScreen(),
debugShowCheckedModeBanner: false,
)
);
}
}
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:scoped_model/scoped_model.dart';
class LikesModel extends Model {
final DocumentSnapshot snapshot;
LikesModel({this.snapshot});
bool _liked = true;
static LikesModel of(BuildContext context) =>
ScopedModel.of<LikesModel>(context, rebuildOnChange: true);
bool isLiked() => _liked;
void pressed(){
_liked = !_liked;
notifyListeners();
}
void changeLikes() async{
await Firestore.instance
.collection("lanchonetes")
.document(snapshot.documentID)
.updateData({'likes': FieldValue.increment(_liked ? -1 : 1)});
}
}
class LanchonetesContact extends StatefulWidget {
final DocumentSnapshot lanchonetes;
LanchonetesContact(this.lanchonetes);
@override
_LanchonetesContactState createState() => _LanchonetesContactState();
}
class _LanchonetesContactState extends State<LanchonetesContact> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 0.0),
child: Card(
elevation: 1.0,
child: GestureDetector(
child: Container(
height: 70.0,
width: 390.0,
color: Colors.white,
child: Row(
children: <Widget>[
Icon(
LikesModel.of(context).isLiked() ?
Icons.favorite_border : Icons.favorite,
color: LikesModel.of(context).isLiked() ?
Colors.black : Colors.red,
size: 50.0,
),
StreamBuilder(
stream: Firestore.instance
.collection('lanchonetes')
.document(widget.lanchonetes.documentID)
.snapshots(),
builder: (context, snapshot) => Text(
snapshot.data.data["likes"].toString(),
style: TextStyle(fontSize: 40.0),
),
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
onTap: () {
LikesModel.of(context).pressed();
LikesModel.of(context).changeLikes();
}
))
),
Which returns me the following information:
════════ (114) Exception caught by widgets library
═════════════════════════════════════════════════
The getter 'data' was called on null.
Receiver: null
Tried calling: data
User-created ancestor of the error-causing widget was:
Row
file:///Users/ricardooscarkanitz/AndroidStudioProjects/e_ai_casimiro/lib/contacts/lanchonetes_contact.dart:79:36
════════════════════════════════════════════════════════════════════════════════════════════════════
Maybe your
document
is not returning any record to thedocumentId
informed... Let us know when you are giving the error– Matheus Ribeiro
@Matheusribeiro believe it is in the documentId same, if I type the specific document name the function works, my snapshot is not calling the Firestore, unfortunately I am beginner in flutter and do not know how to solve.
– Ricardo Oscar Kanitz
Edit your question and let us know a few things: Where do you make use of the method
changeLikes()
? Where does theFieldValue
? Debug your methodchangeLikes()
and show us what comes insnapshot
?– Matheus Ribeiro
@Matheusribeiro the
changeLikes
is used in functiononTap
, theFieldValue
is used to increment a value in Firebase,snapshot
should access the documents of my collectionlanchonetes
as in my classLanchonetesContact
, but I believe you’re not accessing the document for some reason.– Ricardo Oscar Kanitz
I saw that you modified your question as soon as possible I create an answer for you!
– Matheus Ribeiro