[英]How to send data to backend (heroku) for a multi-page signup page using Dio package?
I am trying to write a signup page using Flutter's Dio package. It is multi-page which consists of: selecting your email, school, interest categories, and date of birth.我正在尝试使用 Flutter 的 Dio package 编写一个注册页面。它是多页的,包括:选择您的 email、学校、兴趣类别和出生日期。 User will be proceeded to the next page after completing the previous page.
用户将在完成上一页后进入下一页。 I want all the data to be posted to /user/register API of USER API once the user completes the last page, which is the dob page.
一旦用户完成最后一页,即 dob 页面,我希望将所有数据发布到 USER API 的 /user/register API。 How should I implement such signup page?
我应该如何实现这样的注册页面? Is there any example?
有没有例子? I would like your help.
我需要你的帮助。 Thank you.
谢谢你。
If you can share more details or screenshots about what you want, you can reach much healthier and more accurate answers.如果您可以分享更多关于您想要的内容的详细信息或屏幕截图,您可以获得更健康、更准确的答案。 I'll still try to be as helpful as possible.
我仍然会尽力提供帮助。
You can trigger the code below when clicking the 'Save' button on the register page单击注册页面上的“保存”按钮时,您可以触发下面的代码
Future<bool> userSave(String email, String school, String interestCategories,
DateTime dateOfBirth) async {
try {
const String apiUrl = "your-api-url";
var params = {
"email": email,
"school": school,
"interestCategories": interestCategories,
"dateOfBirth": dateOfBirth
};
Response response = await Dio().post(
apiUrl,
options: Options(headers: {
HttpHeaders.contentTypeHeader: "application/json",
}),
data: jsonEncode(params),
);
if (response.statusCode == 200) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
I prepared an example to be compatible with your screen, you can get the places you need from here我准备了一个与您的屏幕兼容的示例,您可以从这里获取您需要的地方
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController interestCategoriesController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController schoolController = TextEditingController();
DateTime dateOfBirth = DateTime.now();
bool apiResponse = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: mainArea(),
);
}
Widget mainArea() {
return Center(
child: ElevatedButton(
child: const Text("Save"),
onPressed: () async {
apiResponse = await userSave(
emailController.text,
schoolController.text,
interestCategoriesController.text,
dateOfBirth);
if (apiResponse == true) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondPage()),
);
} else {
//You can show alerDialog for response is false
}
},
),
);
}
Future<bool> userSave(String email, String school, String interestCategories,
DateTime dateOfBirth) async {
try {
const String apiUrl = "your-api-url";
var params = {
"email": email,
"school": school,
"interestCategories": interestCategories,
"dateOfBirth": dateOfBirth
};
Response response = await Dio().post(
apiUrl,
options: Options(headers: {
HttpHeaders.contentTypeHeader: "application/json",
}),
data: jsonEncode(params),
);
if (response.statusCode == 200) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.