'Connect Metamask with a native mobile app built with Flutter
I want to connect the app to Metamsk to get the user account and signature from the user to confirm the transaction. But the problem is that when I enter Metamsk from the app, I am not asked any questions to confirm the connection and nothing happens inside Metamsk, it is like only the Metamsk app is opened and when I back up and return to the app, the null account is returned. this is code:
_walletConnect() async {
final connector = WalletConnect(
bridge: 'https://bridge.walletconnect.org',
clientMeta: const PeerMeta(
name: 'WalletConnect',
description: 'WalletConnect Developer App',
url: 'https://walletconnect.org',
icons: [
'https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media'
],
),
);
// Subscribe to events
connector.on('connect', (session) => print(session));
connector.on('session_update', (payload) => print(payload));
connector.on('disconnect', (session) => print(session));
// Create a new session
if (!connector.connected) {
session = await connector.createSession(
chainId: 97,
onDisplayUri: (uri) async => {print(uri), await launch(uri)});
}
setState(() {
account = session.accounts[0];
print(account);
});
if (account != null) {
final client = Web3Client(rpc, Client());
EthereumWalletConnectProvider provider =
EthereumWalletConnectProvider(connector);
credentials = WalletConnectEthereumCredentials(provider: provider);
yourContract = ethUtils.getDeployedContract(myAddress, client);
}
Solution 1:[1]
It sometimes happened to me as well. I had just closed the MetaMask app and clicked on the button (Which triggered this function) and it worked fine.
Solution 2:[2]
I made a library for this, web3_connect, uses the same code and it gets the address back just fine for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Akshay Bhimani |
Solution 2 | RoRo |