这是signup1.dart函数,它将控制器从signup1转移到signup2
void navigateToSignup2() {
if (emailController.text.isNotEmpty &&
fullNameController.text.isNotEmpty &&
phoneNumberController.text.isNotEmpty &&
passwordController.text.isNotEmpty) {
// Passing data from Signup1 to Signup2
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SignUp2(
fullName: fullNameController.text,
email: emailController.text,
phone: phoneNumberController.text,
password: passwordController.text,
),
),
);
} else {
setState(() {
_isNotValidate = true;
});
}
}
这是signup2.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../config.dart';
import '../uihelperbutton/textfield_Name.dart';
import 'signup_1.dart';
class SignUp2 extends StatefulWidget {
final String fullName;
final String email;
final String phone;
final String password;
const SignUp2({
super.key,
required this.fullName,
required this.email,
required this.phone,
required this.password,
});
@override
State<SignUp2> createState() => _SignUp2State();
}
class _SignUp2State extends State<SignUp2> {
final TextEditingController businessNameController = TextEditingController();
final TextEditingController informalNameController = TextEditingController();
final TextEditingController streetAddressController = TextEditingController();
final TextEditingController cityController = TextEditingController();
final TextEditingController zipCodeController = TextEditingController();
final TextEditingController stateController = TextEditingController();
bool _isNotValidate = false;
void registerUser() async {
if (businessNameController.text.isNotEmpty &&
informalNameController.text.isNotEmpty &&
streetAddressController.text.isNotEmpty &&
cityController.text.isNotEmpty &&
zipCodeController.text.isNotEmpty &&
stateController.text.isNotEmpty) {
var regBody = {
"full_name": widget.fullName,
"email": widget.email,
"phone": widget.phone,
"password": widget.password,
"business_name": businessNameController.text,
"informal_name": informalNameController.text,
"address": streetAddressController.text,
"city": cityController.text,
"state": stateController.text,
"zip_code": zipCodeController.text,
};
print("Request body: $regBody"); // Log the request body
print(jsonEncode(regBody));
try {
var response = await http.post(
Uri.parse(registration),
headers: {"Content-Type": "application/json"},
body: jsonEncode(regBody),
);
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
if (response.statusCode == 200) {
print("User registered successfully");
// Navigate to success page or show success message
} else {
print("Error: ${response.statusCode}, ${response.body}");
// Show an error dialog or message to the user
}
} catch (e) {
print("An error occurred: $e");
}
} else {
setState(() {
_isNotValidate = true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(left: 30.0, top: 40),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'FarmerEats',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 25,),
Text('Signup 2 of 4',style: TextStyle(color: Colors.grey),),
SizedBox(
height: 8,
),
Text(
'Farm Info!',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
),
SizedBox(
height: 30,
),
TextfieldName(Image: Image.asset('assets/images/business_name_logo.png',width: 20), hintText: 'Business Name',controller: businessNameController,),
SizedBox(height: 20),
TextfieldName(Image: Image.asset('assets/images/simely_logo.png',width: 20,), hintText: 'Informal Name',controller: informalNameController,),
SizedBox(height: 20),
TextfieldName(Image: Image.asset('assets/images/address_logo.png',width: 20,), hintText: 'Street Address',controller: streetAddressController,),
SizedBox(height: 20),
TextfieldName(Image: Image.asset('assets/images/location_logo.png',width: 20,), hintText: 'City',controller: cityController,),
SizedBox(height: 20,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 50,
width: 125,
decoration: BoxDecoration(
color: Colors.grey.shade200,
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.only(left: 8.0, right: 14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextField(
controller: stateController,
style: TextStyle(color: Colors.black),
textInputAction: TextInputAction.next,
autofocus: false,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'State',
hintStyle: TextStyle(color: Colors.grey),
),
),
),
Image.asset(
'assets/images/Polygon [email protected]',
height: 10,
),
],
),
),
),
Padding(
padding: const EdgeInsets.only(right: 28.0),
child: Container(
height: 50,
width: 190,
child: TextField(
controller: zipCodeController,
style: TextStyle(color: Colors.black),
textInputAction: TextInputAction.next,
autofocus: false,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(color: Colors.grey, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
),
hintText: 'Enter Zipcode',
hintStyle: TextStyle(color: Colors.black12),
fillColor: Colors.grey.shade200,
filled: true,
),
),
),
),
],
),
SizedBox(height: 160),
Padding(
padding: const EdgeInsets.only(right: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Image.asset(
'assets/images/arrow_back.png',
height: 18,
),
),
Container(
width: 225,
height: 45,
decoration: BoxDecoration(
color: Color(0xffD5715B),
borderRadius: BorderRadius.circular(20),
),
child: Center(
child: GestureDetector(
onTap: registerUser,
child: Text(
'Signup',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
),
),
],
),
),
SizedBox(height: 30),
],
),
),
),
);
}
}
我已经尝试过-
headers: {"Content-Type": "application/json"}, 之前 标题:{“Content-Type”:“Application/json”}, 我的移动设备和笔记本电脑连接到同一 wifi 网络 手动添加端口和IP到android studio 我还与邮递员检查过,后端工作正常,数据存储在 mongodb 中。
我不太明白你的错误, 但如果你的情况是: 您正在同一台笔记本电脑上运行后端,并且您正在尝试从 flutter 模拟器访问它,您所要做的就是 http://10.0.2.2:5237/{your end point} 端口号就是您必须根据后端端口更改的所有内容,因为 Android 安全性拒绝访问本地网址