Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

reuse Method in Flutter

So it sound like a dumb question but I don’t get it. I have a Controller Class that has a method

    import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import '../utils.dart';

class UserController {
  Future signMeIn(TextEditingController emailController,
      TextEditingController passwordController, context, navigatorKey) async {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => Center(
              child: CircularProgressIndicator(),
            ));
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: emailController.text.trim(),
          password: passwordController.text.trim());
    } on FirebaseAuthException catch (e) {
      print(e);

      Utils.showSnackBar(e.message);
    }

    navigatorKey.currentState!.popUntil((route) => route.isFirst);
  }
}

and I simply want to reuse this method in a stateful widget

so Im importing the file where the method is defined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import "user_controller.dart";

and trying to call it here

ElevatedButton.icon(
            style: ElevatedButton.styleFrom(minimumSize: Size.fromHeight(50)),
            icon: Icon(Icons.lock_open, size: 32),
            label: Text(
              "Sign In",
              style: TextStyle(fontSize: 24),
            ),
            onPressed: signMeIn(parameters),
          ),

but I get the error that it isn’t defined
what am I doing wrong?

>Solution :

UserController controller = UserController();

ElevatedButton.icon(
            style: ElevatedButton.styleFrom(minimumSize: Size.fromHeight(50)),
            icon: Icon(Icons.lock_open, size: 32),
            label: Text(
              "Sign In",
              style: TextStyle(fontSize: 24),
            ),
            onPressed: controller.signMeIn(parameters),
          ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading