Not able to post http request using TextEditingController variable in JSON body - flutter

I am not able to post http request using TextEditingController in JSON body, but same code is working if I directly pass the string to JSON body
class _FormPageState extends State<FormPage> {
late String name1;
late String email1;
late String phone1;
late String pass1;
late String pass2;
TextEditingController name = new TextEditingController();
TextEditingController email = new TextEditingController();
TextEditingController phone = new TextEditingController();
TextEditingController password = new TextEditingController();
TextEditingController confirmpassword = new TextEditingController();
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
radius: 70,
child: Image.network(
"https://protocoderspoint.com/wp-content/uploads/2020/10/PROTO-CODERS-POINT-LOGO-water-mark-.png"),
),
SizedBox(
height: 15,
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: name,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.person, "Full Name"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter name";
}
return null;
},
onSaved: (String? name) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: email,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.email, "Email"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter email";
}
if (!RegExp("^[a-zA-Z0-9+_.-]+#[a-zA-Z0-9.-]+.[a-z]")
.hasMatch(value)) {
return "Please enter valid email";
}
return null;
},
onSaved: (String? email) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: phone,
keyboardType: TextInputType.number,
decoration: buildInputDecoration(Icons.phone, "Phone No"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter phone";
}
if (value.length < 9) {
return "Please enter valid phone";
}
return null;
},
onSaved: (String? phone) {},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: password,
keyboardType: TextInputType.text,
decoration: buildInputDecoration(Icons.lock, "Password"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter password";
}
return null;
},
),
),
Padding(
padding:
const EdgeInsets.only(bottom: 15, left: 10, right: 10),
child: TextFormField(
controller: confirmpassword,
obscureText: true,
keyboardType: TextInputType.text,
decoration:
buildInputDecoration(Icons.lock, "Confirm Password"),
validator: (String? value) {
if (value!.isEmpty) {
return "Please enter re-password";
}
if (password.text != confirmpassword.text) {
return "Password Do not match";
}
return null;
},
),
),
SizedBox(
width: 200,
height: 50,
child: RaisedButton(
color: Colors.redAccent,
onPressed: () {
if (_formkey.currentState!.validate()) {
RegistrationUser();
print("Successful");
} else {
print("Unsuccessfull");
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
side: BorderSide(color: Colors.blue, width: 2)),
textColor: Colors.white,
child: Text("Submit"),
),
)
],
),
),
),
),
);
}
Future RegistrationUser() async {
var APIURL =
"https://heythere14168465465.000webhostapp.com/registration.php";
name1 = name.text;
email1 = email.text;
phone1 = phone.text;
pass1 = password.text;
pass2 = 'd55';
print(name1);
print(email1);
print(phone1);
print(pass1);
http.Response reponse = await http.post(Uri.parse(APIURL), body: {
"name": name1,
"email": email1,
"phone": phone1,
"password": pass1
});
//getting response from php code, here
var data = jsonDecode(reponse.body);
print("DATA: ${data}");
}
}

Related

Constant display of the message in the else condition in a post request

I am performing a post request after filling out a form. Any time I send the request (i.e. click on the send button), the error message in the else {} condition is what printed out but when I check the backend, the data/response has been sent/received alright.
I really cannot figure out what exactly is the cause here.
This is the entire post request code
class BorrowBook extends StatelessWidget {
BorrowBook({Key? key}) : super(key: key);
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController _titleController = TextEditingController();
TextEditingController _authorController = TextEditingController();
TextEditingController _indexController = TextEditingController();
TextEditingController _dateController = TextEditingController();
TextEditingController _returnController = TextEditingController();
TextEditingController _nameController = TextEditingController();
PostController _postController = Get.put(PostController());
void _clearForm() {
_titleController.clear();
_authorController.clear();
_indexController.clear();
_dateController.clear();
_returnController.clear();
_nameController.clear();
}
// This is the postRequest method
postRequest(userData, url) async {
var response = await _client.post(
Uri.parse(ProjectApis.requestUrl),
body: json.encode(userData),
headers: _projectApis.headers,
);
return response;
}
// This is the sendUserRequest method
sendUserRequest() async {
var userData = {
'title': _titleController.text,
'author': _authorController.text,
'index': _indexController.text,
'borrow_date': _dateController.text,
'return_date': _returnController.text,
'borrowed_by': _nameController.text,
};
var response =
await _postController.postRequest(userData, ProjectApis.requestUrl);
var body = json.decode(response.body);
if (body['status'] == 'success') {
_clearForm();
Get.snackbar(
'Success',
'Your request has been sent successfully',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.green,
margin: EdgeInsets.all(10),
borderColor: Colors.green,
borderWidth: 2,
colorText: Colors.white,
snackStyle: SnackStyle.FLOATING,
duration: Duration(seconds: 2),
);
} else {
Get.snackbar(
'Error',
'Something went wrong',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
margin: EdgeInsets.all(10),
borderColor: Colors.red,
borderWidth: 2,
colorText: Colors.white,
snackStyle: SnackStyle.FLOATING,
duration: Duration(seconds: 2),
);
}
}
#override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
body: SingleChildScrollView(
child: Padding(
padding: ProjectStyle.padding(context),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TopSection(
width: width,
backIcon: Icons.chevron_left_sharp,
onPressed: () {},
text: "Borrow A Book",
height: height,
closeIcon: Icons.cancel_outlined,
),
SizedBox(height: height * 0.02),
Container(
height: height * 0.2,
width: width,
decoration: BoxDecoration(
color: ProjectColors.grey,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
filterQuality: FilterQuality.high,
image: NetworkImage(
"https://images.unsplash.com/photo-1456513080510-7bf3a84b82f8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTB8fGJvb2tzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60",
),
fit: BoxFit.cover,
),
),
),
SizedBox(height: height * 0.02),
Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Book Title",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _titleController,
hintText: "Enter Book Title",
inputType: TextInputType.text,
validate: (value) {
if (value.isEmpty) {
return "Please enter a title";
}
return null;
},
saved: (value) {
_titleController.text = value;
},
),
SizedBox(height: height * 0.02),
Text(
"Author Name",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _authorController,
hintText: "Enter Author Name",
inputType: TextInputType.text,
validate: (value) {
if (value.isEmpty) {
return 'Please enter Author Name';
}
return null;
},
saved: (value) {
_authorController.text = value;
},
),
SizedBox(height: height * 0.02),
Text(
"Student Index",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _indexController,
hintText: "Enter Your Index",
inputType: TextInputType.text,
validate: (value) {
if (value.isEmpty) {
return 'Please enter your index';
}
return null;
},
saved: (value) {
_indexController.text = value;
},
),
SizedBox(height: height * 0.02),
Text(
"Borrow Date",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _dateController,
hintText: "Enter Borrow Date",
inputType: TextInputType.datetime,
validate: (value) {
if (value.isEmpty) {
return 'Please enter Borrow Date';
}
return null;
},
saved: (value) {
_dateController.text = value;
},
),
SizedBox(height: height * 0.02),
Text(
"Return Date",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _returnController,
hintText: "Enter Return Date",
inputType: TextInputType.datetime,
validate: (value) {
if (value.isEmpty) {
return 'Please enter return date';
}
return null;
},
saved: (value) {
_returnController.text = value;
},
),
SizedBox(height: height * 0.02),
Text(
"Borrowed By",
style: ProjectStyle.textStyle(context),
),
SizedBox(height: height * 0.02),
PostFormInputField(
inputController: _nameController,
hintText: "Enter Your Full Name",
inputType: TextInputType.text,
validate: (value) {
if (value.isEmpty) {
return 'Please enter your name';
}
return null;
},
saved: (value) {
_nameController.text = value;
},
),
SizedBox(height: height * 0.02),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: ProjectColors.primary,
minimumSize: Size(width * 0.95, height * 0.08),
),
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
sendUserRequest();
}
},
child: Text("Submit Request"),
),
SizedBox(height: height * 0.02),
],
),
),
],
),
),
),
),
);
}
}
**This is the widget for the input field **
class PostFormInputField extends StatelessWidget {
const PostFormInputField({
Key? key,
required this.inputController,
required this.inputType,
required this.hintText,
required this.validate,
required this.saved,
}) : super(key: key);
final TextEditingController inputController;
final TextInputType inputType;
final String hintText;
final Function validate, saved;
#override
Widget build(BuildContext context) {
return TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: inputController,
keyboardType: inputType,
validator: (value) {
return validate(value);
},
onSaved: (value) {
return saved(value);
},
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: ProjectColors.grey,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: ProjectColors.grey,
),
),
hintText: hintText,
),
);
}
}
I have not consulted any link yet as I have not seen any post with a similar question on this platform yet.
Once again, the main issue is THE MESSAGE IN THE ELSE {} CONDITION IS WHAT KEEPS BEING PRINTED OUT

Exception caught by gesture 'then' was called on null

I faced this error when I tried to insert image
"The following NoSuchMethodError was thrown while handling a gesture:
The method 'then' was called on null.
Receiver: null
Tried calling: then(Closure: (dynamic) => Null)"
Below is my code. Hope someone can help me with it. I have no idea how to solve it. Thank you.
// ignore_for_file: import_of_legacy_library_into_null_safe, unrelated_type_equality_checks
import 'dart:io';
import 'package:dparking/model/user_model.dart';
import 'package:dparking/screens/home_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
class RegistrationScreen extends StatefulWidget {
const RegistrationScreen({Key? key}) : super(key: key);
#override
State<RegistrationScreen> createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _auth = FirebaseAuth.instance;
// form key
final _formKey = GlobalKey<FormState>();
// editing Controller
final nameEditingController = TextEditingController();
final emailEditingController = TextEditingController();
final passwordEditingController = TextEditingController();
final confirmPasswordEditingController = TextEditingController();
final phoneNumberEditingController = TextEditingController();
XFile? _logo;
final ImagePicker _picker = ImagePicker();
#override
Widget build(BuildContext context) {
// Name field
final nameField = TextFormField(
autofocus: false,
controller: nameEditingController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
RegExp regex = RegExp(r'^.{3,}$');
if(value!.isEmpty){
return ("Name cannot be Empty");
}
if(!regex.hasMatch(value))
{
return ("Enter Valid Name(Min. 3 Character");
}
return null;
},
onSaved: (value)
{
nameEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.account_circle),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Name",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
// Email field
final emailField = TextFormField(
autofocus: false,
controller: emailEditingController,
keyboardType: TextInputType.emailAddress,
validator: (value)
{
if(value!.isEmpty)
{
return ("Please Enter Your Email");
}
// reg expression for email validation
if(!RegExp("^[a-zA-Z0-9+_.-]+#[a-zA-Z0-9.-]+.[a-z]").hasMatch(value))
{
return("Please Enter a valid email");
}
return null;
},
onSaved: (value)
{
emailEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.mail),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
// Password field
final passwordField = TextFormField(
autofocus: false,
controller: passwordEditingController,
obscureText: true,
validator: (value) {
RegExp regex = RegExp(r'^.{6,}$');
if(value!.isEmpty){
return ("Password is required for login");
}
if(!regex.hasMatch(value))
{
return ("Enter Valid Password(Min. 6 Character");
}
return null;
},
onSaved: (value)
{
passwordEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.vpn_key),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
// Confirm Password field
final confirmPasswordField = TextFormField(
autofocus: false,
controller: confirmPasswordEditingController,
obscureText: true,
validator: (value)
{
if(confirmPasswordEditingController.text.length !=
passwordEditingController.text){
return "Password Does Not Match";
}
return null;
},
onSaved: (value)
{
confirmPasswordEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.vpn_key),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Confirm Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
//signup button
final signupButton = Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.redAccent,
child: MaterialButton(
padding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
minWidth: MediaQuery.of(context).size.width,
onPressed: () {
signUp(emailEditingController.text, passwordEditingController.text);
},
child: const Text("SignUp", textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold,),
),
),
);
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.red),
onPressed: (){
Navigator.of(context).pop();
},
),
),
body: Center(
child: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 200,
width: 300,
child: Image.asset("assets/images/DParking.png",
fit: BoxFit.contain,
color: Colors.black,
),
),
nameField,
const SizedBox(height: 30),
emailField,
const SizedBox(height: 30),
passwordField,
const SizedBox(height: 30),
confirmPasswordField,
const SizedBox(height: 30),
InkWell(
onTap: (){
_pickImage().then((value){ //i think the error is here
setState((){
_logo = value;
});
});
},
child: Card(
elevation: 4,
child: _logo==null ? const SizedBox(
height: 100,
width: 250,
child: Center(
child: Text('Insert Identification Card'),
),
):SizedBox(
height: 100,
width: 250,
child: Image.file(File(_logo!.path),)
),
),
),
const SizedBox(height: 30),
signupButton,
const SizedBox(height: 30),
],
),
),
),
),
),
)
);
}
void signUp(String email, String password) async
{
if (_formKey.currentState!.validate()) {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => {postDetailsToFirestore()})
.catchError((e) {
Fluttertoast.showToast(msg: e!.message);
});
}
}
postDetailsToFirestore() async {
// call firestore
//call user model
//send value
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
UserModel userModel = UserModel();
userModel.email = user!.email;
userModel.uid = user.uid;
userModel.name = nameEditingController.text;
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => const HomeScreen()),
(route) => false);
}
}
_pickImage() {
}

Adding new roles in the same application

Im trying to add a new sign in role for Rider to sign in to see the orders from customers in the same app but im not sure if im able to add it into my existing code for currently just user to register, sign in and sign up for a new role which is the rider. Would appreciate some advice or suggestion. thank you.
my user model:
class UserModel {
String? uid;
String? email;
String? firstName;
String? secondName;
UserModel({this.uid, this.email, this.firstName, this.secondName});
// receiving data from server
factory UserModel.fromMap(map) {
return UserModel(
uid: map['uid'],
email: map['email'],
firstName: map['firstName'],
secondName: map['secondName'],
);
}
// sending data to our server
Map<String, dynamic> toMap() {
return {
'uid': uid,
'email': email,
'firstName': firstName,
'secondName': secondName,
};
}
}
my sign up:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
//import 'package:grocery_shopping/pages/home/homepage.dart';
import 'package:grocery_shopping/signin.dart';
import 'package:grocery_shopping/user_model.dart';
class SignUp extends StatefulWidget {
const SignUp({Key? key}) : super(key: key);
#override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
final _auth = FirebaseAuth.instance;
// string for displaying the error Message
String? errorMessage;
// our form key
final _formKey = GlobalKey<FormState>();
// editing Controller
final firstNameEditingController = new TextEditingController();
final secondNameEditingController = new TextEditingController();
final emailEditingController = new TextEditingController();
final passwordEditingController = new TextEditingController();
final confirmPasswordEditingController = new TextEditingController();
#override
Widget build(BuildContext context) {
//first name field
final firstNameField = TextFormField(
autofocus: false,
controller: firstNameEditingController,
keyboardType: TextInputType.name,
validator: (value) {
RegExp regex = new RegExp(r'^.{3,}$');
if (value!.isEmpty) {
return ("First Name cannot be Empty");
}
if (!regex.hasMatch(value)) {
return ("Enter Valid name(Min. 3 Character)");
}
return null;
},
onSaved: (value) {
firstNameEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: Icon(Icons.account_circle),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "First Name",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//second name field
final secondNameField = TextFormField(
autofocus: false,
controller: secondNameEditingController,
keyboardType: TextInputType.name,
validator: (value) {
if (value!.isEmpty) {
return ("Second Name cannot be Empty");
}
return null;
},
onSaved: (value) {
secondNameEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: Icon(Icons.account_circle),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Second Name",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//email field
final emailField = TextFormField(
autofocus: false,
controller: emailEditingController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value!.isEmpty) {
return ("Please Enter Your Email");
}
// reg expression for email validation
if (!RegExp("^[a-zA-Z0-9+_.-]+#[a-zA-Z0-9.-]+.[a-z]")
.hasMatch(value)) {
return ("Please Enter a valid email");
}
return null;
},
onSaved: (value) {
firstNameEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: Icon(Icons.mail),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//password field
final passwordField = TextFormField(
autofocus: false,
controller: passwordEditingController,
obscureText: true,
validator: (value) {
RegExp regex = new RegExp(r'^.{6,}$');
if (value!.isEmpty) {
return ("Password is required for login");
}
if (!regex.hasMatch(value)) {
return ("Enter Valid Password(Min. 6 Character)");
}
},
onSaved: (value) {
firstNameEditingController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: Icon(Icons.vpn_key),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//confirm password field
final confirmPasswordField = TextFormField(
autofocus: false,
controller: confirmPasswordEditingController,
obscureText: true,
validator: (value) {
if (confirmPasswordEditingController.text !=
passwordEditingController.text) {
return "Password don't match";
}
return null;
},
onSaved: (value) {
confirmPasswordEditingController.text = value!;
},
textInputAction: TextInputAction.done,
decoration: InputDecoration(
prefixIcon: Icon(Icons.vpn_key),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Confirm Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//signup button
final signUpButton = Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.lightBlueAccent,
child: MaterialButton(
padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
minWidth: MediaQuery
.of(context)
.size
.width,
onPressed: () {
signUp(emailEditingController.text, passwordEditingController.text);
},
child: Text(
"SignUp",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),
)),
);
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.blue),
onPressed: () {
// passing this to our root
Navigator.of(context).pop();
},
),
),
body: Center(
child: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 180,
child: Image.asset(
"assets/grocery shopping logo.png",
fit: BoxFit.contain,
)),
SizedBox(height: 45),
firstNameField,
SizedBox(height: 20),
secondNameField,
SizedBox(height: 20),
emailField,
SizedBox(height: 20),
passwordField,
SizedBox(height: 20),
confirmPasswordField,
SizedBox(height: 20),
signUpButton,
SizedBox(height: 15),
],
),
),
),
),
),
),
);
}
void signUp(String email, String password) async {
if (_formKey.currentState!.validate()) {
try {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => {postDetailsToFirestore()})
.catchError((e) {
Fluttertoast.showToast(msg: "SignUp Successful");
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => SignIn()));
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "invalid-email":
errorMessage = "Your email address appears to be malformed.";
break;
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "user-disabled":
errorMessage = "User with this email has been disabled.";
break;
case "too-many-requests":
errorMessage = "Too many requests";
break;
case "operation-not-allowed":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened.";
}
Fluttertoast.showToast(msg: errorMessage!);
print(error.code);
}
}
}
postDetailsToFirestore() async {
// calling our firestore
// calling our user model
// sedning these values
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
UserModel userModel = UserModel();
// writing all the values
userModel.email = user!.email;
userModel.uid = user.uid;
userModel.firstName = firstNameEditingController.text;
userModel.secondName = secondNameEditingController.text;
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully :) ");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => SignIn()),
(route) => false);
}
}
my sign in:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:grocery_shopping/signup.dart';
import 'package:grocery_shopping/pages/home/homepage.dart';
class SignIn extends StatefulWidget {
const SignIn({Key? key}) : super(key: key);
#override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
// form key
final _formKey = GlobalKey<FormState>();
// editing controller
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
// firebase
final _auth = FirebaseAuth.instance;
// string for displaying the error Message
String? errorMessage;
#override
Widget build(BuildContext context) {
//email field
final emailField = TextFormField(
autofocus: false,
controller: emailController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value!.isEmpty) {
return ("Please Enter Your Email");
}
// reg expression for email validation
if (!RegExp("^[a-zA-Z0-9+_.-]+#[a-zA-Z0-9.-]+.[a-z]")
.hasMatch(value)) {
return ("Please Enter a valid email");
}
return null;
},
onSaved: (value) {
emailController.text = value!;
},
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.mail),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
//password field
final passwordField = TextFormField(
autofocus: false,
controller: passwordController,
obscureText: true,
validator: (value) {
RegExp regex = RegExp(r'^.{6,}$');
if (value!.isEmpty) {
return ("Password is required for login");
}
if (!regex.hasMatch(value)) {
return ("Enter Valid Password(Min. 6 Character)");
}
},
onSaved: (value) {
passwordController.text = value!;
},
textInputAction: TextInputAction.done,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.vpn_key),
contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
));
final loginButton = Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.lightBlueAccent,
child: MaterialButton(
padding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
minWidth: MediaQuery.of(context).size.width,
onPressed: () {
signIn(emailController.text, passwordController.text);
},
child: const Text(
"Sign In",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),
)),
);
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: SingleChildScrollView(
child: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 200,
child: Image.asset(
"assets/grocery shopping logo.png",
fit: BoxFit.contain,
)),
const SizedBox(height: 45),
emailField,
const SizedBox(height: 25),
passwordField,
const SizedBox(height: 35),
loginButton,
const SizedBox(height: 15),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("Don't have an account? "),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const SignUp()));
},
child: const Text(
"SignUp",
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.bold,
fontSize: 15),
),
)
])
],
),
),
),
),
),
),
);
}
// login function
void signIn(String email, String password) async {
if (_formKey.currentState!.validate()) {
try {
await _auth
.signInWithEmailAndPassword(email: email, password: password)
.then((uid) => {
Fluttertoast.showToast(msg: "Login Successful"),
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const CatalogScreen())),
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "invalid-email":
errorMessage = "Your email address appears to be malformed.";
break;
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "user-disabled":
errorMessage = "User with this email has been disabled.";
break;
case "too-many-requests":
errorMessage = "Too many requests";
break;
case "operation-not-allowed":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened.";
}
Fluttertoast.showToast(msg: errorMessage!);
print(error.code);
}
}
}
}

Cannot send data to database after update data

I'm a newbie in using flutter, so this time I want to explain my problem. I created a data update form, when I want to update the data but the data cannot be updated and sent to the database, can friends here help solve my problem? Here is the source code snippet that I made. Thank you all friends. I hope that friends can also understand my question :)
edit_nasabah.dart
class EditNasabah extends StatefulWidget {
Nasabah nasabah;
EditNasabah({this.nasabah});
#override
_EditNasabahState createState() => _EditNasabahState(this.nasabah);
}
class _EditNasabahState extends State<EditNasabah> {
bool _isLoading = false;
final _formKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();
final Nasabah nasabah;
_EditNasabahState(this.nasabah);
bool _secureText = true;
String nama_debitur, alamat, no_telp, no_ktp, no_selular;
showHide() {
setState(() {
_secureText = !_secureText;
});
}
_showMsg(msg) {
final snackBar = SnackBar(
content: Text(msg),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Edit Data"),
),
body: Padding(
padding: EdgeInsets.all(10),
child: ListView(
children: [
SizedBox(
height: 10,
),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.info, size: 50),
title: Text('Edit Form Debitur'),
subtitle: Text('Silahkan edit sesuai form dibawah ini'),
),
],
),
),
SizedBox(
height: 10,
),
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
keyboardType: TextInputType.text,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nama Debitur',
),
validator: (nameValue) {
if (nameValue.isEmpty) {
return 'Please enter your full name';
}
nasabah.nama_debitur = nameValue;
return null;
},
initialValue: "${nasabah.nama_debitur}",
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.text,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Alamat',
),
validator: (alamatValue) {
if (alamatValue.isEmpty) {
return 'Please enter your full name';
}
alamat = alamatValue;
return null;
},
initialValue: "${nasabah.alamat}",
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.text,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nomor Telepon',
),
validator: (notlpValue) {
if (notlpValue.isEmpty) {
return 'Please enter your full name';
}
no_telp = notlpValue;
return null;
},
initialValue: "${nasabah.no_telp}",
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.text,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'NIK',
),
validator: (noktpValue) {
if (noktpValue.isEmpty) {
return 'Please enter your full name';
}
no_ktp = noktpValue;
return null;
},
initialValue: "${nasabah.no_ktp}",
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.text,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'No Selular',
),
validator: (nosllValue) {
if (nosllValue.isEmpty) {
return 'Please enter your full name';
}
no_selular = nosllValue;
return null;
},
initialValue: "${nasabah.no_selular}",
),
SizedBox(
height: 10,
),
Container(
alignment: Alignment.centerRight,
margin: EdgeInsets.all(10),
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
_update();
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(80.0)),
padding: EdgeInsets.all(0.0),
child: Ink(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xff374ABE), Color(0xff64B6FF)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
borderRadius: BorderRadius.circular(30.0)),
child: Container(
constraints:
BoxConstraints(maxWidth: 100.0, minHeight: 50.0),
alignment: Alignment.center,
child: Text(
_isLoading? 'Proccessing..' : 'Simpan',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 15),
),
),
),
),
),
],
),
),
],
),
));
}
This is a function to send data to the database
void _update() async {
setState(() {
_isLoading = true;
});
var data = {nasabah.nama_debitur,nasabah.alamat,nasabah.no_telp,nasabah.no_ktp,nasabah.no_selular};
print(data);
var res = await Network().updateData(data, 'mstdebitur');
var body = json.decode(res.body);
if (res.statusCode == 200) {
SharedPreferences localStorage = await SharedPreferences.getInstance();
localStorage.setString('mstdebitur', json.encode(body['mstdebitur']));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Berhasil Disimpan'),
duration: Duration(seconds: 2),
));
} else {
if (body['message']['nama_debitur'] != null) {
_showMsg(body['message']['nama_debitur'][0].toString());
} else if (body['message']['alamat'] != null) {
_showMsg(body['message']['alamat'][0].toString());
} else if (body['message']['no_telp'] != null) {
_showMsg(body['message']['no_telp'][0].toString());
}
else if (body['message']['no_ktp'] != null) {
_showMsg(body['message']['no_ktp'][0].toString());
}
else if (body['message']['no_selular'] != null) {
_showMsg(body['message']['no_selular'][0].toString());
}
ScaffoldMessenger.of(context)
.showSnackBar(new SnackBar(content: Text("Gagal")));
}
setState(() {
_isLoading = false;
});
}
}
api.dart
updateData(apiURL, id) async {
var fullUrl = _url + apiURL + '/' + id.toString();
await _getToken();
return await http.put(
fullUrl,
headers: _setHeaders(),
);
}
nasabah_service.dart
static Future<List<Nasabah>> updateUser(id) async {
final response = await Network().updateData(baseUrl, id,);
List<Nasabah> list = parseResponse(response.body);
return list;
}
I hope friends can help me.
use TextEditingController for each TextFormField1
create text controller variable:
final TextEditingController _nama_debiturController = TextEditingController();
final TextEditingController _alamatController = TextEditingController();
final TextEditingController _no_telpController = TextEditingController();
final TextEditingController _no_ktpController = TextEditingController();
final TextEditingController _no_selularController = TextEditingController();
you can init value form textController in initState()
#override
void initState() {
super.initState();
_nama_debiturController.text = "hel;lo";
_alamatController.text = "Hi";
_no_telpController.text = "He";
}
And edit your TextFormField widget code:
remove initialValue: ,
add controller: ,
Example:
TextFormField(
keyboardType: TextInputType.text,
controller: _nama_debiturController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nama Debitur',
),
validator: (nameValue) {
if (nameValue!.isEmpty) {
return 'Please enter your full name';
}
nasabah.nama_debitur = nameValue;
return null;
},
),
in your _update()
void _update() async {
var data = {
_nama_debiturController.text,
_alamatController.text,
_no_telpController.text,
_no_ktpController.text,
_no_selularController.text
};
print(data);
}
your Form:
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
keyboardType: TextInputType.text,
controller: _nama_debiturController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nama Debitur',
),
validator: (nameValue) {
if (nameValue!.isEmpty) {
return 'Please enter your full name';
}
nasabah.nama_debitur = nameValue;
return null;
},
),
TextFormField(
keyboardType: TextInputType.text,
controller: _alamatController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Alamat',
),
validator: (alamatValue) {
if (alamatValue!.isEmpty) {
return 'Please enter your full name';
}
alamat = alamatValue;
return null;
},
),
TextFormField(
keyboardType: TextInputType.text,
controller: _no_telpController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nomor Telepon',
),
validator: (notlpValue) {
if (notlpValue!.isEmpty) {
return 'Please enter your full name';
}
no_telp = notlpValue;
return null;
},
),
TextFormField(
keyboardType: TextInputType.text,
controller: _no_ktpController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'NIK',
),
validator: (noktpValue) {
if (noktpValue!.isEmpty) {
return 'Please enter your full name';
}
no_ktp = noktpValue;
return null;
},
),
TextFormField(
keyboardType: TextInputType.text,
controller: _no_selularController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'No Selular',
),
validator: (nosllValue) {
if (nosllValue!.isEmpty) {
return 'Please enter your full name';
}
no_selular = nosllValue;
return null;
},
),
Container(
alignment: Alignment.centerRight,
margin: EdgeInsets.all(10),
child: RaisedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_update();
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(80.0)),
padding: EdgeInsets.all(0.0),
child: Ink(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xff374ABE), Color(0xff64B6FF)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
borderRadius: BorderRadius.circular(30.0)),
child: Container(
constraints:
BoxConstraints(maxWidth: 100.0, minHeight: 50.0),
alignment: Alignment.center,
child: Text(
_isLoading? 'Proccessing..' : 'Simpan',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 15),
),
),
),
),
),
],
),
),
You are missing body of data in put method
updateData(apiURL, id, Map<String, String> data ) async {
var fullUrl = _url + apiURL + '/' + id.toString();
await _getToken();
return await http.put(
fullUrl,
headers: _setHeaders(),
body: jsonEncode(data),
);
}
your data
Map<String, String> data = {
"body1" : _nama_debiturController.text,
"body1": _alamatController.text,
"body1": _no_telpController.text,
"body1" : _no_ktpController.text,
"body1" : _no_selularController.text
};
print(data);
var res = await Network().updateData(data, 'mstdebitur', data);

How to set a value in formTextField

I have a dropdown and a textfield, my goal is when i select a value from dropdown and it will called api, the api will return a string value, and the returned value will be displayed in the textfield. Any help and suggestions will be appreciated in advance.
for example:
when I select "CN" value from the dropdown, the IDDD field will be changed to 86
my code:
class VerifyPhone extends StatefulWidget {
#override
State<StatefulWidget> createState() {
// implement createState
return VerifyPhoneState();
}
}
class VerifyPhoneState extends State<VerifyPhone> {
final _formKey = GlobalKey<FormState>();
String _phone = "";
String _code = "";
String? _iddd = '60';
TextEditingController _idddController = TextEditingController();
List<String> countryCode = ['MY', "CN"];
_showOtpDialog() {
var _form = _formKey.currentState as FormState;
if (_form.validate()) {
_form.save();
showOtpDialog(
context,
_phone,
success: (Map<String, String?>? result) {
if (result != null)
Provider.of<RegistLogic>(context, listen: false)
.doUpdateOtpResult(result);
RoutUtil.push(context, SetupPassword());
},
);
}
}
String? _phoneValidation(String? v) {
return [v.isMalaysiaMobileNo()].validate();
}
#override
void initState() {
super.initState();
_code = countryCode[0];
}
#override
void dispose() {
_idddController.dispose();
super.dispose();
}
_getIddd(String countryCode) async {
final response = await AuthService.countryInfo(
countryCode: countryCode,
);
if (response != null) {
_iddd = response['list01'][0]['int_phone_area_code'];
}
print('idd $_iddd');
}
#override
Widget build(BuildContext context) {
// implement build
return Scaffold(
backgroundColor: Colors.white,
body: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: Stack(
children: <Widget>[
SingleChildScrollView(
child: Column(
children: <Widget>[
// _topTitleBar(context),
AuthHeader(options: HeaderOptions("Phone Number")),
_inputView(),
],
),
),
Positioned(
bottom: 18.0,
left: 20,
right: 20,
child: _postBotton(context),
)
],
),
));
}
Widget _inputView() {
return Container(
width: double.infinity,
margin: EdgeInsets.symmetric(horizontal: 10),
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
'Please enter your phone number to verify your information',
style: TextStyle(color: CustomThemeColor.coolGray)),
),
Column(
children: [
Row(
children: [
Container(
width: 50,
child: DropdownButtonFormField<String>(
decoration: InputDecoration(
labelText: 'Country',
labelStyle: TextStyle(fontSize: 12)),
value: _code,
validator: (v) => [v.isRequired()].validate(),
isDense: true,
onChanged: (val) {
FocusScope.of(context).requestFocus(FocusNode());
_code = val!;
_getIddd(val);
},
items: countryCode.map((item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
),
),
SizedBox(
width: 20,
),
Container(
width: 50,
child: TextFormField(
initialValue: _iddd,
enabled: false,
decoration: InputDecoration(
labelText: "IDDD",
),
onChanged: (value) {
setState(() {
_iddd = value;
});
},
onSaved: (val) {
_iddd = val!;
},
autovalidateMode: AutovalidateMode.onUserInteraction,
),
),
SizedBox(
width: 20,
),
Container(
width: 200,
child: TextFormField(
decoration: InputDecoration(
labelText: "Enter your phone number",
),
onSaved: (val) {
_phone = val!;
},
keyboardType: TextInputType.phone,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(11),
],
validator: _phoneValidation,
autovalidateMode: AutovalidateMode.onUserInteraction,
),
)
],
)
],
)
],
),
));
}
Widget _postBotton(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 10),
padding: EdgeInsets.symmetric(vertical: 15),
child: RaisedButton(
padding: EdgeInsets.symmetric(vertical: 10),
onPressed: () {
_showOtpDialog();
},
color: CustomThemeColor.buttonBlue,
child:
Text('NEXT', style: TextStyle(fontSize: 20, color:
Colors.white)),
));
}
}
Thank you
_getIddd(String countryCode) async {
final response = await AuthService.countryInfo(
countryCode: countryCode,
);
if (response != null) {
_iddd = response['list01'][0]['int_phone_area_code'];
_idddController.text = _iddd;
}
print('idd $_iddd');
}