Home > OS >  Hi I want to be a create keep data of proflie. but had error, How should I do? (I'm a Novice)
Hi I want to be a create keep data of proflie. but had error, How should I do? (I'm a Novice)

Time:10-06

(This is Proflie.dart)

class Proflie{
  String username;
  String password;

  Profile({this.username,this.password});
}

Error Non-nullable instance field 'username' must be initialized. Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'.

Non-nullable instance field 'username' must be initialized. Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'.

'Profile' must have a method body because 'Proflie' isn't abstract. Try making 'Proflie' abstract, or adding a body to 'Profile'.

Initializing formal parameters can only be used in constructors. Try using a normal parameter.

Initializing formal parameters can only be used in constructors. Try using a normal parameter.


(This is Register.dart)

 TextFormField(
                    keyboardType: TextInputType.text,
                    onSaved: (String username){
                      proflie.username = username;
                    },
                  ),

Error

The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'.

CodePudding user response:

Change this

class Proflie{
  String username;
  String password;

  Profile({this.username,this.password});
}

To

class Proflie{
  String username;
  String password;

  Profile({required this.username, required this.password});
}

OR this

class Proflie{
  String? username;
  String? password;

  Profile({this.username,this.password});
}

CodePudding user response:

Class Profile must be called with constructor. It means that you can use this class only this way.

Profile data = Profile(username: usernameVar, password: passwordVar);

Best solution I can advice to create class at the register button and add TextEditingControllers to your fields.

ElevatedButton(
  onTap: () => fetchRegistration(Profile(username: nameController.text, password: passwordController.text)
)...

and for fields:

 TextEditingController nameController = TextEditingController();
 return ...
 
 TextFormField(
   keyboardType: TextInputType.text,
   controller: nameController,
   onSaved: (String username){
     proflie.username = username;
   },
 )...
  • Related