I used the Provider to take the value from the Text Form Field and store it via the Provider but I am getting an error that I could not solve at all. And this code
Code of text_field
import 'dart:ffi';
import 'dart:ui';
import 'package:flutter/material.dart';
Widget text_field( {
required TextEditingController TextEditing_Controller,
required bool obscure_Text,
required TextInputType keyboardType,
required bool auto_focus,
required bool This_Suffix,
required String prefix_Text,
required on_changed,
suffix_Icon,})
//------------------------------------
{
return Container(
//---------------------------
decoration: BoxDecoration(
color: Colors.white,
border: Border(
top: BorderSide(color: Colors.red),
bottom: BorderSide(color: Colors.red),
),
),
//-------------------------------
width: 300,
height: 50,
child:TextFormField(
style: TextStyle(
color: Colors.blue,
fontSize: 15,
),
//----------------------------------
onChanged: on_changed,
controller: TextEditing_Controller,
obscureText:obscure_Text,
keyboardType:keyboardType ,
//----------------------------------
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
//-----------------------------------
decoration: InputDecoration(
border: InputBorder.none,
hintText: prefix_Text,
hintStyle: TextStyle(
color: Colors.blue,
fontSize: 15,
fontWeight: FontWeight.bold
),
fillColor: Colors.white.withOpacity(0.5),
filled: true,
hoverColor: Colors.deepPurpleAccent.withOpacity(0.5),
//-----------------------------------
),
));
}
Code of main This is the main.dart file and from it I called the CV Provider using multiprovider
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(
create: (BuildContext context) => CV(),
),
],
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(
color: Colors.purpleAccent,
iconTheme: IconThemeData(color: Colors.red),
actionsIconTheme: IconThemeData(color: Colors.amber),
centerTitle: false,
elevation: 15,
// backgroundColor:Colors.lightBlue
titleTextStyle: TextStyle(color: Colors.lightBlueAccent),
),
primarySwatch: Colors.blue,
),
home: homeScreen(),
);
}
}
code CVChangeNotifier
This is the CV file that inherited from ChangeNotifire and in which I defined the variables and functions that I used in the form
class CV with ChangeNotifier {
List Skills = [];
var _your_fullname,
_skill,
_your_email,
_your_location,
_your_nameuniversiy,
_your_GPA,
_your_number,
_project;
List projects = [];
//----------------------------------------
get Skill => _skill;
addSkill(Skill) {
_skill = Skill;
Skills.add(_skill);
notifyListeners();
}
//****************************
get Project => _project;
addproject(Project) {
_project = Project;
Skills.add(_project);
notifyListeners();
}
//****************************
get fullname => _your_fullname;
addFullname(fullname) {
_your_fullname = fullname;
notifyListeners();
}
//-----------------------------
get email => _your_fullname;
addEmail(email) {
_your_email = email;
notifyListeners();
}
//-----------------------------
get location => _your_location;
addlocation(location) {
_your_location = location;
notifyListeners();
}
//-----------------------------
get nameuniversiy => _your_nameuniversiy;
addnameuniversiy(nameuniversiy) {
_your_nameuniversiy = nameuniversiy;
notifyListeners();
}
//-----------------------------
get number => _your_number;
addnnumber(number) {
_your_number = number;
notifyListeners();
}
//-----------------------------
get GPA => _your_GPA;
addGPA(GPA) {
_your_GPA = GPA;
notifyListeners();
}
//-----------------------------
}
code of homescreen Here I made a screen to enter information and used initState and knew an object from the Provider
class homeScreen extends StatefulWidget {
@override
State<homeScreen> createState() => _homeScreenState();
}
//==============================================
class _homeScreenState extends State<homeScreen> {
//==============================================
final _formKey = GlobalKey<FormState>();
late TextEditingController _nameController,
_emailController,
_numberController,
_locationController,
_universitnameController,
_GPAController,
_projectController,
_SkillController;
late CV MyCV;
//==============================================
@override
void initState() {
//==============================================
Future.delayed(Duration.zero, () {
MyCV = Provider.of<CV>(context, listen: false);
super.initState();
_nameController = TextEditingController(text: MyCV.fullname);
_emailController = TextEditingController(text: MyCV.email);
_numberController = TextEditingController(text: MyCV.number);
_locationController = TextEditingController(text: MyCV.location);
_universitnameController =
TextEditingController(text: MyCV.nameuniversiy);
_GPAController = TextEditingController(text: MyCV.GPA);
_projectController = TextEditingController(text: MyCV.Project);
_SkillController = TextEditingController(text: MyCV.Skill);
//==============================================
});
}
@override
void dispose() {
//==============================================
_nameController.dispose();
_emailController.dispose();
_numberController.dispose();
_locationController.dispose();
_universitnameController.dispose();
_GPAController.dispose();
_projectController.dispose();
_SkillController.dispose();
//==============================================
super.dispose();
}
@override
Widget build(BuildContext context) {
//==============================================
return Scaffold(
body: SafeArea(
child: Container(
color: Colors.black12,
width: double.infinity,
height: double.infinity,
child: Column(
children: [
Container(
width: double.infinity,
height: 90,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 6),
shape: BoxShape.circle,
)),
Container(
height: 2,
width: 90,
color: Colors.red,
),
SizedBox(
width: 5,
),
Text(
'Personal Info',
style: TextStyle(
color: Colors.blue,
fontSize: 20,
fontWeight: FontWeight.bold),
),
SizedBox(
width: 5,
),
Container(
height: 2,
width: 90,
color: Colors.red,
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 6),
shape: BoxShape.circle,
)),
],
),
),
SizedBox(
height: 10,
),
text_field(
obscure_Text: false,
prefix_Text: 'Your name:',
TextEditing_Controller: _nameController,
on_changed: MyCV.addFullname(_nameController.text),
This_Suffix: false,
auto_focus: true,
keyboardType: TextInputType.text),
SizedBox(
height: 10,
),
text_field(
obscure_Text: false,
prefix_Text: 'Your Email:',
TextEditing_Controller: _emailController,
on_changed: MyCV.addEmail(_emailController.text),
This_Suffix: false,
auto_focus: true,
keyboardType: TextInputType.emailAddress),
SizedBox(
height: 10,
),
text_field(
obscure_Text: false,
prefix_Text: 'Your name of University:',
TextEditing_Controller: _universitnameController,
on_changed:
MyCV.addnameuniversiy(_universitnameController.text),
This_Suffix: false,
auto_focus: true,
keyboardType: TextInputType.text),
SizedBox(
height: 10,
),
text_field(
obscure_Text: false,
prefix_Text: 'Your Location:',
TextEditing_Controller: _locationController,
on_changed: MyCV.addlocation(_locationController.text),
This_Suffix: false,
auto_focus: true,
keyboardType: TextInputType.number),
SizedBox(
height: 10,
),
text_field(
obscure_Text: false,
prefix_Text: 'Your GPA:',
TextEditing_Controller: _GPAController,
on_changed: MyCV.addnameuniversiy(_GPAController.text),
This_Suffix: false,
auto_focus: true,
keyboardType: TextInputType.text),
],
),
),
),
);
}
}
CodePudding user response:
It is better to use addPostFrameCallback
to get context after 1st build.
The issue is here you are passing super.initState();
inside your future delay.
Also For
Follow this way There are controllers are null first build.
TextEditingController _nameController = TextEditingController();
//... others are same
CV? MyCV;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
MyCV = Provider.of<CV>(context, listen: false);
_nameController.text = MyCV?.fullname ?? "";
//... others are same
});
}
on_changed: MyCV?.addFullname(_nameController.text),