'How to display Flushbar from login api error in flutter
I have a login system working with Flutter, PHP, and SQL but I want to be able to display the errors (if any) as a Flushbar. The controller receives the error as (res["message"]) and i would like this to be displayed on the register page but i cant seem to get it to work
Here is the controller that is called on click
void register() async {
try {
var response = await http
.post(Uri.parse("http://noddospls/untitled/authentication/register.php"), body: {
"name" : nameTE.text,
"username": emailTE.text,
"password": passwordTE.text
});
print(response.body);
var res = json.decode(response.body);
var success = (res["success"]);
print (success);
if (formKey.currentState!.validate()) {
if (success == "false"){
//display error from api (res["message"])
} else if (success == "true") {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (context) => LogInScreen(),
),
);
}
}
}catch(e){
print(e);
}
}
and here is my page that the controller is called from
Widget build(BuildContext context) {
return FxBuilder<RegisterController>(
controller: controller,
builder: (controller) {
return Scaffold(
body: Padding(
padding: FxSpacing.fromLTRB(
20, FxSpacing.safeAreaTop(context) + 20, 20, 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FxText.h3(
'Sign up',
fontWeight: 700,
),
FxSpacing.height(20),
Form(
key: controller.formKey,
child: Column(
children: [
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.user,
color: theme.colorScheme.onBackground,
),
hintText: "Name",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.nameTE,
validator: controller.validateName,
cursorColor: theme.colorScheme.onBackground,
),
FxSpacing.height(20),
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.mail,
color: theme.colorScheme.onBackground,
),
hintText: "Email Address",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.emailTE,
validator: controller.validateEmail,
cursorColor: theme.colorScheme.onBackground,
),
FxSpacing.height(20),
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.lock,
color: theme.colorScheme.onBackground,
),
hintText: "Password",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.passwordTE,
validator: controller.validatePassword,
cursorColor: theme.colorScheme.onBackground,
),
FxSpacing.height(20),
TextFormField(
style: FxTextStyle.b2(),
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
filled: true,
isDense: true,
fillColor: customTheme.card,
prefixIcon: Icon(
FeatherIcons.lock,
color: theme.colorScheme.onBackground,
),
hintText: "Re-type Password",
enabledBorder: outlineInputBorder,
focusedBorder: outlineInputBorder,
border: outlineInputBorder,
contentPadding: FxSpacing.all(16),
hintStyle: FxTextStyle.b2(),
isCollapsed: true),
maxLines: 1,
controller: controller.password2TE,
validator: controller.validatePassword,
cursorColor: theme.colorScheme.onBackground,
),
],
),
),
FxSpacing.height(24),
Row(
children: [
FxText.b3(
'By Signing up, you agree to our ',
fontSize: 11,
),
FxText.b3(
'Terms & Conditions',
color: customTheme.fitnessPrimary,
fontSize: 11,
),
],
),
FxSpacing.height(24),
Row(
children: [
FxButton(
padding: FxSpacing.xy(16, 12),
onPressed: () {
controller.register();
},
backgroundColor: customTheme.card,
splashColor:
theme.colorScheme.onBackground.withAlpha(40),
elevation: 0,
borderRadiusAll: 4,
child: Row(
children: [
Image(
image: AssetImage(Images.google),
height: 17,
width: 17,
),
FxSpacing.width(20),
FxText.l2(
'Login with Google',
fontWeight: 600,
color: theme.colorScheme.onBackground,
),
],
)),
FxSpacing.width(20),
Expanded(
child: FxButton(
padding: FxSpacing.y(12),
onPressed: () {
controller.register();
},
backgroundColor: customTheme.fitnessPrimary,
elevation: 0,
borderRadiusAll: 4,
child: FxText.b2(
'Continue',
color: customTheme.fitnessOnPrimary,
fontWeight: 600,
),
),
),
],
),
FxSpacing.height(20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FxText.b3(
'Already have an account? ',
),
InkWell(
onTap: () {
controller.goToLogInScreen();
},
child: FxText.b3(
'Log In',
color: customTheme.fitnessPrimary,
)),
],
),
],
),
),
);
});
}
}
Any help appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
