Embedded Wallets SDK for Flutter
Overview
MetaMask Embedded Wallets SDK (formerly Web3Auth Plug and Play) provides a seamless authentication experience for Flutter applications with social logins, external wallets, and more. Our Flutter SDK, written in Dart, simplifies connecting users to their preferred wallets and manage authentication state across both iOS and Android platforms.
Requirements
- Android API version 26 or newer
- iOS 14, Xcode 12+ and Swift 5.x
- Basic knowledge of Dart and Flutter Development
Prerequisites
- Set up your project on the Embedded Wallets dashboard
See the dashboard setup guide to learn more.
Installation
To install the Web3Auth Flutter package, you have two options. You can either manually add the package in the pubspec.yaml file, or you can use the flutter pub add command.
Add via pubspec.yaml
Add web3auth_flutter as a dependency to your pubspec.yaml:
dependencies:
web3auth_flutter: ^6.1.2
Add via Flutter pub add
Add web3auth_flutter using flutter pub add command:
flutter pub add web3auth_flutter
1. Android configuration
Once we have installed Web3Auth Flutter SDK, we also need to add the configuration for Android.
Update compileSdkVersion
For Android build compileSdkVersion needs to be 34. Check your app module Gradle file in your project to change it:
android {
namespace "com.example.app_name"
compileSdkVersion 34
// ..
}
Add Embedded Wallets to Gradle
In your project-level Gradle file add JitPack repository:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" } // <-- Add this line
}
}
Update permissions
Open your app's AndroidManifest.xml file and add the following permission. Please make sure the <uses-permission> element should be a direct child of the <manifest> root element:
<uses-permission android:name="android.permission.INTERNET" />
Configure deep link
Open your app's AndroidManifest.xml file and add the following deep link intent filter to your Main activity:
<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="{scheme}" android:host="{YOUR_APP_PACKAGE_NAME}"/>
<!-- Accept URIs: w3a://com.example.w3aflutter -->
</intent-filter>
Handle custom tabs
For Android, you need to handle custom tabs lifecycle to trigger login exceptions. The Android SDK uses chrome custom tabs, and it's not possible to add a listener directly to the chrome custom tab close button to trigger login exceptions.
class LoginScreen extends StatefulWidget {
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> with WidgetsBindingObserver {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
void didChangeAppLifecycleState(final AppLifecycleState state) {
// This is important to trigger the user cancellation on Android.
if (state == AppLifecycleState.resumed) {
Web3AuthFlutter.setCustomTabsClosed();
}
}
}
2. iOS configuration
Update global iOS platform
For iOS build global platform needs to be 14.0. Check Podfile in your project to change the global platform:
platform :ios, '14.0'