Flutter Web3: Smart Contract Interaction in Dart
Dart is one of the most versatile programming languages for building dApps and this article will show how easy it is to interact with smart contracts in Dart, call functions and listen to events.
Using Web3dart to generate smart contract dart bindings.
The web3dart package comes with a very cool generator that allows you to generate dart classes from ABIs. While this may not work on super complex smart contracts it goes a long way to simplify the process by using build_runner to create functions and classes from ABI/bytecode.
We start by creating our pubspec.yaml with the following dependencies
Next, we compile our smart contract using either hardhat, truffle, or any solidity compiler you prefer to get the abi
<a href="https://medium.com/media/82fe58dbdb020721eb6ea09bf81cdaca/href">https://medium.com/media/82fe58dbdb020721eb6ea09bf81cdaca/href</a>
We can then place our ABI inside a contracts folder making sure to rename it with the .abi.json extension.
Next we dart run build_runner build -v
This will generate a Lock.g.dart file with the different methods, classes, and events found in the ABI of the smart contract.
Next, we deploy the smart contract to a test chain using hardhat https://hardhat.org/hardhat-runner/docs/getting-started#overview
Once we have deployed the smart contract to localhost we can easily write a dart script to test that we can interact properly with the deployed smart contract:
<a href="https://medium.com/media/26b5e2a88524a6c00ce281a2f46928ea/href">https://medium.com/media/26b5e2a88524a6c00ce281a2f46928ea/href</a>
We can access the different functions and events that exist in our dart implementation of the Lock smart contract.
By running dart main.dart we can easily see that we have been able to interact seamlessly with our deployed smart contract.
Well, that was pretty straightforward, in the next article I’ll go into details about listening to events, handling transactions, and interacting with ERC20, ERC721 smart contracts EIPS.
P.S I’m currently job hunting have a peek at me on LinkedIn.
Flutter Web3: Smart Contract Interaction in Dart was originally published in CoinsBench on Medium, where people are continuing the conversation by highlighting and responding to this story.









