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:

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

  1. 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

  1. 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

  1. 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

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

  • 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.

Similar Reads

Writing your first Smart Contract

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

Compiling and Migrating

...

Testing the Smart Contract

...

Contract linking with Flutter

...

Creating a UI to interact with the smart contract

...

Interacting with the complete Dapp

Compilation...