Building Modern Web Dapp with Flutter
Tags: blockchain, dapps, flutter, smartcontracts
Published: 2021-06-26 02:15:46

---
title: Building Modern Web Dapp with Flutter
published: true
date: 2021-06-25 19:38:22 UTC
tags: blockchain,dapps,flutter,smartcontracts
canonical_url: https://medium.com/coinmonks/building-modern-web-dapp-with-flutter-91656c71d8b
---

#### No more React! No more JS!(jk) We are now in the Flutter era!

### Introduction

Today I will show you how to build a modern web Dapp with Flutter!

We will use the [flutter\_web3](https://github.com/y-pakorn/flutter_web3) package for interacting with the Ethereum object, connecting with Metamask, Wallet Connect, and other providers.

This package also includes other nice utilities to connect to providers like Ethers Js and Wallet Connect.

Talk no more, let's get started!

### Setting Up

First, create a Flutter project.

```
flutter create coolassname
```

And add some package into pubspec.yaml. This time I’ll be using [Getx](https://pub.dev/packages/get) for simple state management, [Niku](https://pub.dev/packages/niku) for cool property builder, and our package [flutter\_web3](https://github.com/y-pakorn/flutter_web3).

```
dependencies:
  flutter:
    sdk: flutter

  flutter_web3:
  niku:
  get:
```

And start Flutter with

```
flutter run -d web-server
```

Because extensions like Metamask can’t be used in Chrome debug browser (sad).

> Keep in mind that the Flutter web server doesn’t have the hot restart feature that automatically refreshes your page, after refreshing in CLI you need to manually refresh your browser again.

### **Building The Website**

#### Planning The Features

The topic we will cover today is

- Using the Ethereum object and its method like requestAccount.
- Using Ethers js to interact with smart contracts.
- Using default ContractERC20 to construct the contract that has ERC20 standard.

Note that these are not all the package features; make sure to check the documentation out for all APIs and features.

So we’ll try to make Pancake Swap Interactor then, for the sake of simplicity.

#### Connect To Account

We first define the basic structure of the website. Then we’ll work on the web3 part after that.

![](https://cdn-images-1.medium.com/max/688/1*eX-Bz7UbuHGD7rlefIOwYQ.png)

Define some extensions to make life easier.

![](https://cdn-images-1.medium.com/max/558/1*92skH1gCqqjJgHNHZoKvxg.png)

For the Web3 part, We’ll put it in HomeController .

![](https://cdn-images-1.medium.com/max/739/1*adbODraBtizsOfb5gysSIA.png)

For explanation, let’s go line by line.

- Line 2, Check if we can access the Ethereum object getter. From the implementation below, the Etherum object can be null if the browser doesn’t have any provider available. i.e. Metamask, Wallet Connect, Binance Chain Wallet, etc.

![](https://cdn-images-1.medium.com/max/636/1*kOApTaDrMAec4IiP9VFxCQ.png)

- Line 4, Check for the chain that is currently connected to is Binance Smart Chain or not (chain id 56).
- Line 6, Overall check for connection to the website. ethereum.isConnected() is only used for provider availability, so we have to check from the address that is exposed from the node.
- Line 9 and 11 is to store the state variable.
- Line 15, Connect to provider and update currentAddress and currentChain . By using ethereum.requestAccount we can prompt the provider to make the connection with the website. (i.e. Metamask modal) that returns the list of addresses, the first address in that list is the current address that will be used. Be sure to handle errors if the user rejects the modal (In this case I don’t, which I shouldn’t do.) And using ethereum.getChainId to update currentChain
- Line 26, Handle accountChanged and chainChanged events. This will call clear every time the user changed his account or chain and update the variable above.

Then we can build a super simple UI to show that we’re connected or not. If not, click a connect button to prompt the provider to connect.

![](https://cdn-images-1.medium.com/max/610/1*cEJ0J0eC6vF4cKNNqG6dMg.png)

So we got the account connection part completed. I will only show crude UI implementation here, you guys can make it your own as good as you want.

![](https://cdn-images-1.medium.com/max/1019/1*6-gO6EPIWsi8-oFXf5QQQg.png)<figcaption>Connect Button</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*P_YvcMGvaEayUdww6yddsQ.png)<figcaption>Connecting to the provider</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*jGoH-K06RxdwXzC8ty5R0w.png)<figcaption>Connected</figcaption>

#### Connect To Ethers js Provider

There are many providers in Ethers js, two of them are Web3Provider and JsonRPCProvider.

Both can be used to read data from the Blockchain, Web3Provider instantiates by putting Ethereum provider into the constructor. JsonRPCProvider instantiates by putting RPC Url into the constructor.

For simplicity, We can connect and get Web3 by using the default provider getter. The implementation is below.

![](https://cdn-images-1.medium.com/max/869/1*RyQeVSefzI9bKNCZY3Fxng.png)

#### Interacting With Contract

We’ll divide this into two sections, interacting with Cake token contract and interacting with the Masterchef contract.

For the Cake token contract, we’ll use ContractERC20 .

For the Masterchef contract, we’ll use normal Ethers Contract and specify our own ABI.

#### Cake Token Contract

It’s super simple! Just instantiate ContractERC20 class with the token address.

We’ll also add an end function that burns 1 gwei of your Cake! Spicy!

![](https://cdn-images-1.medium.com/max/826/1*iAUC4FYGfmLmmPBn8biy-A.png)

We’ll go line by line here again.

- Line 3, 5 stores our variable about the Cake token and our token balance.
- Line 7, Instantiate cakeToken object if null. And get our Cake balance from balanceOf function.
- Line 15, Burn 1 gwei of Cake from your pocket. First, check if your balance is more than 1 gwei (So you have enough to burn and have some left, haha!) then transfer your cake into addressZero , or 0x0. Then update your cake balance again.

To build the UI for our contract, we first need to make sure that the Ethereum object is not null and then build it normally.

![](https://cdn-images-1.medium.com/max/696/1*otc-eRY2kdsiEuzNXeonSA.png)

![](https://cdn-images-1.medium.com/max/1023/1*PY3pe6nznnUKdW-thoXZnw.png)<figcaption>Fetch cake and burn cake button</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*e77J5UIVtV8ymyBvZhmmsg.png)<figcaption>After fetching Cake</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*MQpf_xmzTDCTrqM9lB6krw.png)<figcaption>Burning 1 gwei of Cake</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*gEAHNA_bbCh-pMdzMHDZDQ.png)<figcaption>Burned</figcaption>

#### **Masterchef**  **Contract**

Since we need to create our own Contract instance, we’ll also need the contract’s abi. We can look this up at block explorer of your choice.

We can look into the code of the contract using [BscScan](https://bscscan.com/address/0x73feaa1ee314f8c655e354234017be2193c9e24e#code).

![](https://cdn-images-1.medium.com/max/730/1*NbL7_HORR61uDA7Qfig8fA.png)

We will select some property, constant function and write function to be our example.

First, we need to obtain the abi. For abi specification and type, please refer at [Ethers js documentation](https://docs.ethers.io/v5/api/utils/abi/formats/).

We’ll use minimal human-readable abi here for simplicity, You can also use JSON abi that generated from solidity and paste it here.

So referring to the docs, our abi is.

![](https://cdn-images-1.medium.com/max/592/1*eWOvT3k2dUmHqRmuNbGQtw.png)

Then we can create our Contract and call that function. Note that all function that have uint return type is actually BigNumber in Dart.

![](https://cdn-images-1.medium.com/max/869/1*Sh3xk4oFLCbK-8Dwz3PUMQ.png)

For explanation,

- Line 16, Create Masterchef contract if null, and get cakePerBlock and poolLength with annotated type BigNumber and converted that to int .
- Line 24, Call emergencyWithdraw with 0 indicating pool number as argument.

After that, we proceed to build our UI.

![](https://cdn-images-1.medium.com/max/739/1*muejP7qBltnc1Gy-nUnVgA.png)

![](https://cdn-images-1.medium.com/max/1024/1*cu_eH-qoEniSspRxzs2lDw.png)<figcaption>Fetch information button</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*QQ-3yWLM8OSInSM0L3sSdg.png)<figcaption>After fetching information</figcaption>

### Conclusion

Although Flutter ecosystem for developing Dapp is nowhere to be seen before. But through Dart2js and many Flutter packages that is popping up to strengthen cryptography and web3 utility, this make Flutter a Strong contender for Dapp development in the Future.

Crude example that I’ve shown already prove that Flutter really has the potential to develop complex Dapp using Dart’s extensive capability.

You can find source code for this example [here](https://gist.github.com/y-pakorn/76e0defd1f3cd7861c64c813760ef5d1).

> Join [Coinmonks Telegram Channel](https://t.me/coincodecap) and learn about crypto trading and investing

#### Also, Read

- [YouHodler vs CoinLoan vs Hodlnaut](https://medium.com/coinmonks/youhodler-vs-coinloan-vs-hodlnaut-b1050acde55a)
- [Dollar-Cost Averaging Trading Bot](https://blog.coincodecap.com/pionex-dca-bot)

* * *