Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

3
How to implement separation of concerns?
Post Flair (click to view more posts with a particular flair)
Post Body

I have three files.

  1. add_user
  2. add_user_controller
  3. 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:

  1. How can my saveToDatabase inform the controller if the operation was successful or not with the firebase call?
  2. How can my controller method receive the result of firebase call and inform the UI page?

    Minor issues are:

  3. 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.

  4. 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.

Author
Account Strength
90%
Account Age
8 years
Verified Email
Yes
Verified Flair
No
Total Karma
3,352
Link Karma
759
Comment Karma
2,442
Profile updated: 5 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
8 months ago