I am trying to connect the backend(python) and frontend(flutter). Currently, I am connecting Signin Function.
Python Codes
- user type
- fastapi (signin function) code
class User(BaseModel):
user_name: str
real_name: str
password: str
phone_number: str
email: str
car_number: int
car_color: str
car_type: str
# car_license: str
homeroom: str
user_type: str
warning: list[str]
penalty: int
@router.post("/create")
async def create_user(new_user: User):
data = new_user.dict()
hashed_pw = Hash.bcrypt(new_user.password)
data["password"] = hashed_pw
result = router.database.user.insert_one(data)
return {"message": "User registration successful."}
Flutter Codes
- save(fastapi using through http.post code), saveTwo(fastapi using through dio pub dev)
- How I am using the functions by using onpressed
Future save(String inputusername, String inputName, String inputpassword,
String inputphoneNumber, String inputemail, String inputhomeroom) async {
final Map<String, dynamic> userData = {
'userName': inputusername,
'realName': inputName,
'password': inputpassword,
'phoneNumber': inputphoneNumber,
'email': inputemail,
'carNumber': 0, // An integer, not a string
'carColor': '0',
'carType': '0',
'homeroom': inputhomeroom, // Assuming inputhomeroom is a string
'userType': 'Passenger',
'warning': ['0'], // An empty list of strings
'penalty': 0, // An integer, not a string
};
await http.post(Uri.parse('http://10.0.2.2:8000/user/create'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(userData));
}
Future saveTwo(String inputusername, String inputName, String inputpassword,
String inputphoneNumber, String inputemail, String inputhomeroom) async {
final Map<String, dynamic> userData = {
'userName': inputusername,
'realName': inputName,
'password': inputpassword,
'phoneNumber': inputphoneNumber,
'email': inputemail,
'carNumber': 0, // An integer, not a string
'carColor': '0',
'carType': '0',
'homeroom': inputhomeroom, // Assuming inputhomeroom is a string
'userType': 'Passenger',
'warning': ['0'], // An empty list of strings
'penalty': 0, // An integer, not a string
};
await dio.post('http://10.0.2.2:8000/user/create', data: {
'userName': inputusername,
'realName': inputName,
'password': inputpassword,
'phoneNumber': inputphoneNumber,
'email': inputemail,
'carNumber': 0,
'carColor': '0',
'carType': '0',
'homeroom': inputhomeroom,
'userType': 'Passenger',
'warning': ['0'],
'penalty': 0
}
);
}
TextButton(
child: Text("Signin", style: TextStyle(color: Colors.white)),
onPressed: () {
save(
idController.text,
nameController.text,
pwController.text,
phoneController.text,
emailController.text,
homeroomController.text);
}
),
TextButton(
child: Text("Signin", style: TextStyle(color: Colors.white)),
onPressed: () {
saveTwo(
idController.text,
nameController.text,
pwController.text,
phoneController.text,
emailController.text,
homeroomController.text);
}
),
I have tried into two different ways(save, saveTwo), but it only shows 422 error. I could not figure out whether I put wrong variable type of not. Please help me.
PS. I run python code on the unvicorn docs, and it worked.
API expects fields in snake case (
user_name
) while camel case (userName
) is sent.