0

I'm new to this async/await problem in Flutter. How do I wait until the mnemonic is generated then only I pass it into the generatePrivateKey. After that I would like to show the _getKey after the private key is obtained.

class _MyHomePageState extends State<MyHomePage> {
  String mnemonic = GenAddress().generateMnemonic();
  final Future<String> _getKey = GenAddress().getPrivateKey(mnemonic);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTextStyle(
        style: Theme.of(context).textTheme.displayMedium!,
        child: FutureBuilder<String>(
          future: _getKey, // a previously-obtained Future<String> or null
          builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
            List<Widget> children;
            if (mnemonic != "") {
              children = <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Text('Result: $_getKey'),
                ),
              ];
            } else if (snapshot.hasError) {
              children = <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Text('Error'),
                ),
              ];
            } else {
              children = const <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 16),
                  child: Text('Awaiting result...'),
                ),
              ];
            }
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: children,
              ),
            );
          },
        ),
      ),
    );
  }
}

Here are my code for generateMnemonic and getPrivateKey

class GenAddress implements GetAddressETH {
  String hdPath = "m/44'/60'/0'/0";

  @override
  String generateMnemonic() {
    return bip39.generateMnemonic();
  }

  @override
  Future<String> getPrivateKey(String mnemonic) async {
    final isValidMnemonic = bip39.validateMnemonic(mnemonic);
    if(!isValidMnemonic) {
      throw 'Invalid mnemonic';
    }
    final seed = bip39.mnemonicToSeed(mnemonic);
    final root = bip32.BIP32.fromSeed(seed);

    const first = 0;
    final firstChild = root.derivePath("$hdPath/$first");
    final privateKey = HEX.encode(firstChild.privateKey as List<int>);

    return privateKey;
  }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Leo
  • 179
  • 9

1 Answers1

0

you have to wrap the mnemonic and privateKey method inside a single function:

class _MyHomePageState extends State<MyHomePage> {

  String getMnemonic()=> GenAddress().generateMnemonic();

Future<String> _getKey() async {
final mnemonic = getMnemonic();
  final privateKey = await GenAddress().getPrivateKey(mnemonic);
  return privateKey;
}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTextStyle(
        style: Theme.of(context).textTheme.displayMedium!,
        child: FutureBuilder<String>(
          future: _getKey(), // a previously-obtained Future<String> or null
          builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
            List<Widget> children;
            if (getMnemonic() != "") {
              children = <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Text('Result: $_getKey'),
                ),
              ];
            } else if (snapshot.hasError) {
              children = <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Text('Error'),
                ),
              ];
            } else {
              children = const <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 16),
                  child: Text('Awaiting result...'),
                ),
              ];
            }
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: children,
              ),
            );
          },
        ),
      ),
    );
  }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
john
  • 1,438
  • 8
  • 18