Writing your first Smart Contract
The Smart Contract actually acts as the back-end logic and storage for our Dapp.
- Create a new file named HelloWorld.sol in the contracts/ directory.
- Add the following content to the file:
Solidity
pragma solidity ^0.5.9; contract HelloWorld { } |
- 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
- Add the following variable on the next line after contract HelloWorld {
Solidity
string public yourName ; |
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
- Add the following constructor on the next line after string public yourName ;
Solidity
constructor() public { yourName = "Unknown" ; } |
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
- Add the following function to the smart contract after the constructor declaration we set up above.
Solidity
function setName(string memory nm) public { yourName = nm ; } |
- In the above function, we’ll be taking in an nm(string) and setting the yourName variable to it.
- Memory is the data location.
Flutter and Blockchain – Hello World Dapp
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:
- Setting up the development environment
- Creating a Truffle Project
- Writing your first Smart Contract
- Compiling and Migrating the Smart Contract
- Testing the Smart Contract
- Contract linking with Flutter
- Creating a UI to interact with the smart contract
- 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
- Create a basic Flutter project in your favorite IDE
- Initialize Truffle in the flutter project directory by running
truffle init
Directory Structure
- 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.