This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have three files.
- add_user
- add_user_controller
- add_user_database
add_user.dart has textfields and a submit button. So it's the view.
It passes the text of the textfields to a static method in add_user_controller.dart called "sendToDatabase".
static sendToDatabase({
required String name,
required String mobileNumber,
required String previousAmount,
}) {
final double temp = double.parse(previousAmount);
final String temp2 = temp.toStringAsFixed(2);
final double previousAmountTo2Fractions = double.parse(temp2);
AddNewUserDatabase.saveToDatabase(
name: name,
mobileNumber: mobileNumber,
previousAmount: previousAmountTo2Fractions,
);
}
"add_user_controller.dart" calls a static method in "add_user_database.dart" called "saveToDatabase".
static void saveToDatabase({
required String name,
required String mobileNumber,
required double previousAmount,
}) async {
// Reference to the collection
final CollectionReference collection =
FirebaseFirestore.instance.collection("test");
// First, Make sure the mobile number is not registered to another use
final Query query = collection.where("mobile\_number", isEqualTo: mobileNumber);
final QuerySnapshot doesExist = await query.get();
if (doesExist.size != 0) {
print("This mobile number is already registered with another user");
return;
}
// Now that the number is not registered to another user, save this new user to
the database
await collection.doc().set(
{
"name": name,
"mobile_number": mobileNumber,
"current_account": previousAmount,
"created_at": FieldValue.serverTimestamp(),
"last_transaction": FieldValue.serverTimestamp(),
"last_payment": null,
},
);
}
I'm very unsatisfied with my work here.
Major issues are:
- How can my saveToDatabase inform the controller if the operation was successful or not with the firebase call?
How can my controller method receive the result of firebase call and inform the UI page?
Minor issues are:
I don't feel that using static method is good, but I don't know why I feel that way or if a better way
exists.Is there a better way to do this all over? I feel there should a better (and easier) way to do this.
It should be clear, but I'm super noob with writing well structured code. I can write messy code all day long and this is the first time I want to write well structured code.
Thank you in advance.
Subreddit
Post Details
- Posted
- 8 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/flutterhelp...