DQtech Column | Learn from DEX about PlatON Application Development – Flutter (I)

Author Dex_DQT

From now on we will start to build the Flutter version of Digging. In this chapter we will be setting up the environment and build the function for getting the wallet balance.

There is no official SDK for Flutter, but on the basis of the json-rpc encapsulated interface for Android, we could know that all we need to do in Flutter is to serialize/deserialize the rlp and sign it using ECDSA.

Build the Project

A reference on how to set up the environment can be found on FlutterChina

Let’s start creating the project. Since Flutter is cross-platform, I will create the project on windows in this chapter, and switch to mac later.

Open Android Studio, then open File->New->New Flutter Project, as shown below

1644415560(1)

Select the Flutter SDK, I’m using version 2.8.1, as shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (I)

Check the box for IOS and Android in the configuration panel.

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (I)

Once the project has been created, the directory would be as follows:

1644415828(1)

What in sdk package is shown as below:

1644415966(1)

The sdk was written by me with reference to the java version of the sdk, so the package and class names are basically the same as the java version.

Create a secret key using the SDK

ThThe _MyHomePageState code in main.dart is as follows:

class _MyHomePageState extends State<MyHomePage> {
  String _address = "";

  void _generateWallet() async {
    // 创建钱包
    ECKeyPair ecKeyPair = WalletUtil.generatePlatONBip39Wallet();

    Credentials credentials =  Credentials.createByECKeyPair(ecKeyPair);

    setState(() {
      _address = credentials.address;
      print(_address);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_address), // 显示钱包地址
            ElevatedButton(
              child: const Text("按钮"),
              onPressed: () {
                _generateWallet();
              },
            ),
          ],
        ),
      ),
    );
  }
}

The wallet is generated in the _generateWallet function via WalletUtil.generatePlatONBip39Wallet, as shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (I)

It can be seen from the diagram that the wallet address we generated is: lat19ydcsmfnxlqw4640u93r3chn2pk0c8shsn3lhg

Pick up the LAT at the test network to that address. Then write the code to get the balance of the wallet, which is as follows:

... 省略代码
  void _getBalance() async {
    // 创建钱包
    BigInt balance =
        await _web3.getBalance(_address);

    setState(() {
      _lat = AmountUtil.convertVonToLat(balance);
    });
  }
... 省略代码

we need to initialise the network parameters in the main function with the following code before that :

void main() {
  // 使用测试网
  NetworkParameters.init(BigInt.from(210309), "lat");
  //
  runApp(const MyApp());
}

Initialize the Web3 instance in the _MyHomePageState class. The code is as follows:

... 省略代码
  // 创建web3对象
  final Web3 _web3 = Web3.build("http://35.247.155.162:6789");
... 省略代码
DQtech Column | Learn from DEX about PlatON Application Development – Flutter (I)

You will be able to see the balance of the wallet we have obtained.

Well, that’s all of this chapter.

The most important thing in Flutter is the implementation of the application, and the functionality of the SDK will be gradually improved in this series of article. As for the source code of the SDK, it would not be focused on.

The URL of github: https://github.com/DQTechnology/Platon_DevGuideProject

This article is reproduced from https://forum.latticex.foundation/t/topic/6020

Like (0)
PlatOnWorld-Kita's avatarPlatOnWorld-KitaOfficial
Previous March 29, 2022 10:37
Next March 30, 2022 11:48

相关推荐

Leave a Reply

Please Login to Comment