skip to Main Content

The variables of the model should be private to give it greater security in some way or should they not be private?

the help was greatly appreciated

code

— flutter —

List FavoritosUsuarioFromJson(String str) => List<InfoModel>.from(json.decode(str).map((x) => InfoModel.fromJson(x)));

class InfoModel{
  String name;
   String email;

  InfoModel({required this.name,required this.email});

  factory InfoModel.fromJson(Map<String, dynamic> parsedJson){
    return InfoModel(

        name: parsedJson['usu_name'],
         email: parsedJson['usu_email'],
    );
  }

}

2

Answers


  1. In general, it is a good practice to make variables private in order to encapsulate and protect the internal state of a class. By making variables private, you restrict direct access from outside the class, which can help prevent unintended modifications and ensure proper data handling.

    In your code example, the variables name and email in the InfoModel class are not marked as private, so they are accessible from outside the class. If you want to make them private, you can add an underscore (_) before their names:

        class InfoModel {
          String _name;
          String _email;
        
          InfoModel({required String name, required String email})
              : _name = name,
                _email = email;
        
          // Rest of the class implementation...
        }
    

    By making the variables private, other parts of your code will need to use getter and setter methods to access or modify these variables. This provides better control over how the variables are accessed and manipulated.

    However, it’s important to note that making variables private alone doesn’t guarantee absolute security. It’s just one aspect of maintaining the integrity of your code. Depending on the context and requirements of your application, you may need to consider additional security measures, such as input validation, encryption, access control, and more, to ensure the overall security of your system.

    Login or Signup to reply.
  2. I don’t think that its mandatory to make the model private as it holds the business logic in MVC away from the presentation layer, and the methods for retrieving data should be accessible from the controller.

    Any way you should make the methods or variables that will be used internally inside the model private.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search