I have a method which writes a profile of child (person) to Cloud Firestore. There are couple of collections and documents I create in Firestore for this such as emotion counters with initial values of zero (for further purposes). The function became pretty complex to create the whole profile of a person.
Unfoortunately, it works very unstable. Sometimes quickly, sometimes it takes forever or never. Probably I am doing something wrong as I heard that Firebase is quick and efficient.
Please have a look at the syntax - does it look ok?
Thanks in advance for your help.
Code of the method:
Future<void> addNewChild(
childFacilityId,
childFacility,
childGroup,
childGroupId,
childFirstName,
childLastName,
childImageFile,
isHorizontalImage,
context) async {
final scaffold = ScaffoldMessenger.of(context);
final _timeStamp = DateTime.now();
final user = FirebaseAuth.instance.currentUser;
final ref = FirebaseStorage.instance
.ref()
.child('assets')
.child('userImages')
.child(
'$_timeStamp' '_' + childLastName + '_' + childFirstName + '.png');
scaffold.showSnackBar(SnackBar(
backgroundColor: Theme.of(context).primaryColor,
content: Text(
'Creating child's profile started...',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25, color: Colors.white54),
),
));
try {
await FirebaseFirestore.instance
.collection('root')
.doc('users')
.collection('userData')
.doc(user!.uid)
.get()
.then((value) async {
updateAccountId(value['accountId']);
updateActualFacilityId(value['facilityId']);
print('AddNewChild STEP1');
print(
'ADD NEW CHILD VARIALBLES accountId: $accountId, facilityId: $childFacilityId');
await FirebaseFirestore.instance
.collection('root')
.doc('accounts')
.collection('accountData')
.doc(accountId)
.collection('facilities')
.doc(facilityId)
.collection('children')
.add({
'childId': '',
}).then((_) async {
print('AddNewChild STEP2');
final _childId = value.id;
await ref.putFile(childImageFile);
final _userImage = await ref.getDownloadURL();
final _userImageUrl = _userImage.toString();
final childIdReference = FirebaseFirestore.instance
.collection('root')
.doc('accounts')
.collection('accountData')
.doc(accountId)
.collection('facilities')
.doc(facilityId)
.collection('children')
.doc(_childId);
//creating child
childIdReference.set({
'childId': _childId,
'childAccountId': accountId,
'childFacilityId': childFacilityId,
'childFirstName': childFirstName,
'childLastName': childLastName,
'childGroupName': childGroup,
'groupId': childGroupId,
'childImageUrl': _userImageUrl,
'childFacility': childFacility,
'timeStamp': _timeStamp,
'isHorizontalImage': isHorizontalImage,
'timerStateIsOn': false,
}).then((_) async {
print('AddNewChild STEP3');
//creating counters for child
//counter for 1 day
await childIdReference
.collection('counters')
.doc('counters_1day')
.set({
'angerHigh': 0,
'angerLow': 0,
'angerMedium': 0,
'calmHigh': 0,
'calmLow': 0,
'calmMedium': 0,
'childFirstName': childFirstName,
'childImageUrl': _userImageUrl,
'childLastName': childLastName,
'fearHigh': 0,
'fearLow': 0,
'fearMedium': 0,
'joyHigh': 0,
'joyLow': 0,
'joyMedium': 0,
'sadnessHigh': 0,
'sadnessLow': 0,
'sadnessMedium': 0,
'timeStampLastUpdated': _timeStamp,
}).then((_) async {
print('AddNewChild STEP4');
//counter for 7 days
await childIdReference
.collection('counters')
.doc('counters_7days')
.set({
'angerHigh': 0,
'angerLow': 0,
'angerMedium': 0,
'calmHigh': 0,
'calmLow': 0,
'calmMedium': 0,
'childFirstName': childFirstName,
'childImageUrl': _userImageUrl,
'childLastName': childLastName,
'fearHigh': 0,
'fearLow': 0,
'fearMedium': 0,
'joyHigh': 0,
'joyLow': 0,
'joyMedium': 0,
'sadnessHigh': 0,
'sadnessLow': 0,
'sadnessMedium': 0,
'timeStampLastUpdated': _timeStamp,
});
}).then((_) async {
print('AddNewChild STEP5');
//counter for 7 days
await childIdReference
.collection('counters')
.doc('counters_30days')
.set({
'angerHigh': 0,
'angerLow': 0,
'angerMedium': 0,
'calmHigh': 0,
'calmLow': 0,
'calmMedium': 0,
'childFirstName': childFirstName,
'childImageUrl': _userImageUrl,
'childLastName': childLastName,
'fearHigh': 0,
'fearLow': 0,
'fearMedium': 0,
'joyHigh': 0,
'joyLow': 0,
'joyMedium': 0,
'sadnessHigh': 0,
'sadnessLow': 0,
'sadnessMedium': 0,
'timeStampLastUpdated': _timeStamp,
});
}).then((value) async {
print('AddNewChild STEP6');
scaffold.showSnackBar(SnackBar(
backgroundColor: Colors.green,
content: Text(
'Successfully added $childFirstName $childLastName',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25),
),
));
print(
'AddNewChild - succesfully added $childFirstName $childLastName');
});
});
});
});
} catch (e, stacktrace) {
scaffold.showSnackBar(SnackBar(
backgroundColor: Colors.red,
content: Text(
'Something went wrong. Couldn't create profile for $childFirstName $childLastName',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25, color: Colors.white60),
),
));
print('ERROR AddNewChild: $e, STACKTRACE: $stacktrace');
}
}