Search code examples
flutterazureauthenticationazure-ad-b2c

MissingPluginException When Using aad_oauth for Microsoft Multi-Factor Authentication in Flutter


I am developing a Flutter application that uses the aad_oauth package to implement Microsoft multi-factor authentication. However, I am encountering a MissingPluginException error during the login process. The error message is as follows:

I/flutter (18709): Login failed: MissingPluginException(No implementation found for method delete on channel plugins.it_nomads.com/flutter_secure_storage)

Here is the relevant part of my code where I configure and initiate the login process:

class _LoginScreenState extends State<LoginScreen> {
  final TextEditingController _emailController = TextEditingController();
  late final AadOAuth oauth;

  @override
  void initState() {
    super.initState();
    final config = Config(
      tenant: '2f5964b2-44fc-420d-8974-xxxx',
      clientId: 'ed4c80e3-847b-4b0a-85a9-xxxxx',
      scope: 'openid profile email',
      redirectUri: 'msauth://com.xxxxxx/auth',
      navigatorKey: LoginScreen.navigatorKey,
    );
    oauth = AadOAuth(config);
  }

  void _login() async {
    try {
      await oauth.login();
      final accessToken = await oauth.getAccessToken();
      if (accessToken != null) {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => SuccessScreen()),
        );
      }
    } catch (e) {
      print('Login failed: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(24.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [

            SizedBox(height: 20),
            Text(
              'Sign in',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 20),
            TextField(
              controller: _emailController,
              decoration: InputDecoration(
                labelText: 'Email, phone, or Skype',
                border: OutlineInputBorder(),
                filled: true,
                fillColor: Colors.white,
              ),
              keyboardType: TextInputType.emailAddress,
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: _login,
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 12),
                child: Text(
                  'Next',
                  style: TextStyle(fontSize: 16),
                ),
              ),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue[700],
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(4),
                ),
              ),
            ),
            SizedBox(height: 16),
            TextButton(
              onPressed: () {},
              child: Text('Can\'t access your account?'),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _emailController.dispose();
    super.dispose();
  }
}

in android manifest added

  <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:scheme="msauth"
                    android:host="com.xx.xx" />
            </intent-filter>

How can I resolve this MissingPluginException? Has anyone else encountered this issue with the aad_oauth package, or is there a step I might be missing in the setup? Any insights or solutions would be greatly appreciated.


Solution

  • This package aad_oauth has a dependency name flutter_secure_storage enter image description here And flutter_secure_storage have a native method name delete

    So I guess when you use oauth.login() or oauth.getAccessToken(), the native method delete will be called, but somehow in native code, this method didn't exist, that's why you got the error

    MissingPluginException(No implementation found for method delete on channel plugins.it_nomads.com/flutter_secure_storage)

    So let's search keyword to check whether your project downloaded the dependency success

    Native method "delete" on Android: enter image description here

    on iOS: enter image description here

    If your project doesn't have them, you may try clean flutter or pub cache, then re-sync the dependencies.