I am having a problem of reading a number from firebase. I have no problems writing the number to firebase but as soon as I close the app the numbers displayed on screen go back to zero. So I'm trying to read the data from firebase then display that data as a number on screen.. however I need it to be an integer not a string as if the user clicks the button its supposed to Add 1 to the number... and I'm at a lost here is the code I'm using to get the string: I've Since cleaned up the code hopefully this will make it easier:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:counter/Components/GetUserData.dart';
import 'package:intl/intl.dart';
final DateTime now = DateTime.now();
final DateFormat yearFormatter = DateFormat.y();
final DateFormat monthFormatter = DateFormat.M();
final DateFormat dayFormatter = DateFormat.d();
final path = FirebaseFirestore.instance
.collection(officeCode)
.doc(firstName)
.collection(personalCode)
.doc('Numbers')
.collection('Numbers')
.doc(monthFormatter.format(now)
'-'
dayFormatter.format(now)
'-'
yearFormatter.format(now));
getCurrentEngaged() async {
await path.get().then(
(doc) {
currentEngaged = int.parse((doc.data()! as Map)['Engaged'].toString());
},
);
}
var currentEngaged = getCurrentEngaged();
now all I'm doing right now is just trying to read it from firebase. I'm getting this error now I'm using
print(currentEngaged);
and in my console I'm seeing Instance of 'Future<dynamic>'
Edit after printing it 2x on the second time printing it displays the data
CodePudding user response:
Try this:
engaged = int.parse((doc.data()! as Map)['Engaged'].toString());
CodePudding user response:
ok so here is the code that I have that is now working so thank you for your help
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:counter/Components/GetUserData.dart';
import 'package:intl/intl.dart';
final DateTime now = DateTime.now();
final DateFormat yearFormatter = DateFormat.y();
final DateFormat monthFormatter = DateFormat.M();
final DateFormat dayFormatter = DateFormat.d();
final path = FirebaseFirestore.instance
.collection(officeCode)
.doc(firstName)
.collection(personalCode)
.doc('Numbers')
.collection('Numbers')
.doc(monthFormatter.format(now)
'-'
dayFormatter.format(now)
'-'
yearFormatter.format(now));
getCurrentEngaged() async {
await path.get().then(
(doc) {
currentEngaged = int.parse((doc.data()! as Map)['Engaged'].toString());
},
);
}
var currentEngaged = getCurrentEngaged();
that is all on in one file. In my main file I am then making a private int to take the place of the current information in firebase with this code:
int _engaged = int.parse(currentEngaged.toString());
im then displaying the number with this code (its a custom text widget)
textWidget(
text: currentEngaged.toString(),
size: 40,
weight: FontWeight.w900,
),
and I have a custom button to update the number in firebase which looks like this:
NumberButtons(
color: minus,
icon: Icons.remove_circle,
pressed: () {
setState(() {
if (_engaged == 0) {
return;
} else {
_engaged--;
}
});
path.set(
{
"Engaged": _engaged,
},
);
},
),
I really hope this helps someone now the only thing is this doesn't rebuild the app so the number doesn't change in the app unless its closed and re opened or hot restarted. I'm still working on this and if I get it before anyone else I will edit this post to include that