Home > database >  Flutter best practice for form submissions
Flutter best practice for form submissions

Time:11-22

I am having trouble finding good resources for what best practices would be for Flutter development, specifically for form handling.

Everything I find on form submissions is fairly clear, but the problem is they all have the validation logic and submission logic directly in the form widget. I don't like this as it seems it would get very convoluted very quickly with more than say 3 inputs and any sort of more than basic validation logic. It also seems to violate the separation of concerns thinking that I though was supposed to be a big thing in Flutter/Dar (at least from what I have read).

So my chosen solution for this was my FormHandler class, which I defined in the form_handler.dart file. It has some static methods for validation of input, some methods for submission handling, and a formInput of type Map<String, dynamic> for storing key value pairs of user input.

It works like this:

  1. An instance of the FormHandler is created
  2. The user inputs the data
  3. On form.save(), for each user input, the input data is stored in the formInput map, with key being the title of the input, and the value being the user's input.
  4. The submission button would run the validation and save functions and then take the data from formInput and send it to something like a database handler that would store it on the db

form_handler.dart:

class FormHandler {
  // make new form handler with empty map
  FormHandler({required this.formInput});


  // for storing input key value pairs
  Map<String, dynamic> formInput;




  // Form submissions
  // new course
  void submitCourse({required formKey}){
    final form = formKey.currentState;

    // save on validate
    if( form.validate() ){
      form.save();

      // then make new course via the database controller
    }
  }

  // Input validations
  static String? validateTextInput(String? input){
    if( input == null || input.isEmpty ){
        return 'Field must not be empty';
    } else {
      return null;
    }
  }
}

I'm just wondering if this is a good solution, what are some potential pitfalls, any suggestions etc.

It seems like a good solution to me, but I would like feedback from someone with more experience than me.

Thanks, Seth.

CodePudding user response:

You can refer to this youtube channel for guidance. Here is a link for a video related to TextFields and Validation - https://youtu.be/2rn3XbBijy4

CodePudding user response:

This is unfortunately, in my opinion, a very opinionated question without a clear true/false answer, and by the SO guidenlines, such questions should be closed.

With that said, to give a hint before this question is closed.. A common practice would be to create value objects or entities that hold logic to do validation on themselves. Perhaps with an interface (abstract class) as a base for those to make sure that you don't forget to implement such validation.

Thereby moving such validation logic from the UI and instead caring about what is important in your domain. It will be possible to validate in several places in your code, not just from the form, which is not something easily achievable with your proposed solution. Your solution still mix UI with logic via the formKey. You might want to validate "stuff" when you receive data from your backend, or do other calculations/changes to values later in the app.

  • Related