Blockchain

Flutter and Blockchain

This tutorial will take you through the process of building your first mobile dapp – Hello World Dapp!

This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of the Flutter framework but are new to mobile dapps.

In this tutorial we will be covering:

  1. Setting up the development environment
  2. Creating a Truffle Project
  3. Writing your first Smart Contract
  4. Compiling and Migrating the Smart Contract
  5. Testing the Smart Contract
  6. Contract linking with Flutter
  7. Creating a UI to interact with the smart contract
  8. Interacting with the complete Dapp

Setting up the development environment

Truffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier. But before we install truffle make sure to install node .

Once we have node installed, we only need one command to install Truffle:

npm install -g truffle

We will also be using Ganache, a personal blockchain for Ethereum development you can use to deploy smart contracts, develop applications, and run tests. You can download Ganache by navigating to http://truffleframework.com/ganache and clicking the “Download” button.

Creating a Truffle Project

  1. Create a basic Flutter project in your favorite IDE
  2. Initialize Truffle in the flutter project directory by running

truffle init

Directory Structure

Screenshot480.png

  • contracts/ : Contains solidity contract file.
  • migrations/: Contains migration script files (Truffle uses a migration system to handle contract deployment).
  • test/ : Contains test script files.
  • truffle-config.js: Contains truffle deployment configurations information.

Writing your first Smart Contract

The Smart Contract actually acts as the back-end logic and storage for our Dapp.

  1. Create a new file named HelloWorld.sol in the contracts/ directory.
  2. Add the following content to the file:
  • The minimum version of Solidity required is noted at the top of the contract: pragma solidity ^0.5.9;.
  • Statements are terminated with semicolons.

Variable Setup

  1. Add the following variable on the next line after contract HelloWorld {

We just defined a single variable yourName of type string, also yourName is a public modifier which means that we can access it from outside the smart contract.

Constructor

  1. Add the following constructor on the next line after string public yourName ;

Constructor in solidity executed only once, when a contract is created and it is used to initialize contract state. Here we’re just setting the initial value of variable yourName to “Unknown”.

Function

  1. Add the following function to the smart contract after the constructor declaration we set up above.
  • In the above function, we’ll be taking in an nm(string) and setting the yourName variable to it.
  • Memory is the data location.

Compiling and Migrating

Compilation

  1. In a terminal, make sure you are in the root of the directory that contains the flutter and truffle project, Run the following command:

truffle compile

You should see output similar to the following:

Migration

You’ll see one JavaScript file already in the migrations/ directory: 1_initial_migration.js. This handles deploying the Migrations.sol contract to observe subsequent smart contract migrations, and ensures we don’t double-migrate unchanged contracts in the future.

Let’s create our own migration script :

  1. Create a new file named 2_deploy_contracts.js in the migrations/ directory.
  2. Add the following content to the 2_deploy_contracts.js file:
  • Before we can migrate our contract to the blockchain, we need to have a blockchain running. For this article, we’re going to use Ganache, a personal blockchain for Ethereum development you can use to deploy contracts, develop applications, and run tests. If you haven’t already, download Ganache and double-click the icon to launch the application. This will generate a blockchain running locally on port 7545.
  • Add the following content to the truffle-config.js file:
  • Migrating the contract to the blockchain, run:

truffle migrate

You should see output similar to the following:

  • Take a look into the Ganache, the first account originally had 100 ether, it is now lower, due to the transaction costs of migration.

Testing the Smart Contract

In Truffle, we can write tests either in JavaScript or Solidity, In this article, we’ll be writing our tests in Javascript using the Chai and Mocha libraries.

  1. Create a new file named helloWorld.js in the test/ directory.
  2. Add the following content to the helloWorld.js file:
  • HelloWorld: The smart contract we want to test, We begin our test by importing our HelloWorld contract using artifacts.require.
  • To test the setName function, recall that it accepts a name(string) as an argument.
  • Also, the yourName variable in our contract is using public modifier, which we can use as a getter from the outside function.
  • Truffle imports Chai so we can use the assert function. We pass the actual value and the expected value, To check that the name is set properly or not, assert(result === “User Name”) ;.

Running the tests

  • Running the test as:

truffle test

  • If all the test pass, you’ll see the console output similar to this:

Contract linking with Flutter

  • In the pubspec.yaml file import the following packages :

provider: ^4.3.3 web3dart: ^1.2.3 http: ^0.12.2 web_socket_channel: ^1.2.0

  • Also, add the asset src/artifacts/HelloWorld.jsonto pubspec.yaml file which is generated by truffle-config.js while we migrate our contract.

assets: – src/artifacts/HelloWorld.json

  1. Create a new file named contract_linking.dart in the lib/ directory.
  2. Add the following content to the file:
  • Just a simple class with ChangeNotifier for state management.

Variables

  • Add the following variable on the next line after class ContractLinking extends ChangeNotifier {.

The library web3dart won’t send signed transactions to miners itself. Instead, it relies on an RPC client to do that. For the WebSocket URL just modify the RPC URL. You can get the RPC URL from the ganache :

  • Get the Private Key from ganache:
  • Declare the following variables below :
  1. _client variable will be used to establish a connection to the ethereum rpc node with the help of WebSocket.
  2. isLoading variable will be used to check the state of the contract.
  3. _abiCode variable will be used to, read the contract abi.
  4. _contractAddress variable will be used to store the contract address of the deployed smart contract.
  5. _credentials variable will store the credentials of the smart contract deployer.
  6. _contract variable will be used to tell Web3dart where our contract is declared.
  7. _yourName and _setName variable will be used to store the functions declared in our HelloWorld.sol smart contract.
  8. deployedName will hold the name from the smart contract.

Functions

  • After declaring the above variable’s, Declare the following functions below it :

Creating a UI to interact with the smart contract

  1. Create a new file named helloUI.dart in the lib/ directory.
  2. Add the following content to the file:
  • Update the main.dart as :

Interacting with the complete Dapp

  • Now we’re ready to use our dapp!
  • Just RUN the Flutter Project.

As you can see the Hello Unknown, in the UI is actually coming from the smart contract, variable yourName .

When you type your name into the TextFormField and Press `Set Name` ElevatedButton, it will invoke the setName function from contract_linking.dart which will directly invoke the setName function of our smart contract (HelloWorld.sol).

Congratulations! You have taken a huge step to become a full-fledged mobile dapp developer. For developing locally, you have all the tools you need to start making more advanced dapps.

If you stuck somewhere, do check out the GitHub repository for complete code.

Related Articles

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker