0

I currently have a password reset page on my app.

The widget call a password reset function when the button is pressed. I have the reset button inside a Builder. I have test a snackbar inside the onPressed and it woks fine . But I want to be able to use the Firebase auth error inside the snackbar if the password has an error. but when trying to call the snackbar is still get a scaffold context error. my code is below. i am not putting the full widget tree as it is quite long but have put down what i think are the important part. The code to update the password runs fine, so I don't think there is any blocker there.

class _PasswordBuyerState extends State<PasswordBuyer> {

final newpassword = TextEditingController();
final confirmpassword = TextEditingController();
final _PasswordformKey = GlobalKey<FormState>();

void SubmitBuyerPassword(password) async{

 var user = await FirebaseAuth.instance.currentUser();

 user.updatePassword(password).then((_){

   print("Succesfully changed password");

   final snackbar = SnackBar(content: Text("Password changes Successfully"));
   Scaffold.of(context).showSnackBar(snackbar);
 // this is not showing

   Navigator.push(context,MaterialPageRoute(builder: (context) => EditProfileBuyer()));

 }).catchError((error){

   final snackbar = SnackBar(content: Text("Password can't be changed" + error.toString()));

   Scaffold.of(context).showSnackBar(snackbar);  // If the form is valid, display a Snackbar.
 // this is not showing

   print("Password can't be changed" + error.toString());

 });

  }
  @override
  Widget build(BuildContext context) {
return MaterialApp(
  home: Scaffold(

Further down in the code is the Builder with the button:

child:Builder(
builder: (context) => RaisedButton(
onPressed: (){

if (_PasswordformKey.currentState.validate()) {

if(newpassword.text == confirmpassword.text){

print('matching');

setState(() {

final snackbar = SnackBar(content: Text('password changing'));

Scaffold.of(context).showSnackBar(snackbar);
// the above snackbar works when tested

SubmitBuyerPassword(newpassword.text.toString());

});

} else if(newpassword.text != confirmpassword.text){

 print('not matching');


}

};

},

color: Colors.blue[500],

child:  Text('Submit',textAlign: TextAlign.center,style: TextStyle(color:Colors.white),),
                                ),
                              ),
Waseem Ahmed
  • 379
  • 1
  • 5
  • 17
  • 1
    i think you should just use a package like [Flushbar](https://pub.dev/packages/flushbar) or [flash](https://pub.dev/packages/flash) and don't have to worry about scaffold context. – hewa jalal May 08 '20 at 10:45
  • 1
    @hiwa, Flushbar is awesome and so simple, as as a answer and I will upvote your answer – Waseem Ahmed May 08 '20 at 11:42
  • i posted it as an answer [here](https://stackoverflow.com/a/61704017/11385677). – hewa jalal May 09 '20 at 21:31

1 Answers1

0

See answer above by @Hiwa Jala. use the Flushbar or flash extension.

I personally used Flushbar due to its simplicity.

Waseem Ahmed
  • 379
  • 1
  • 5
  • 17