Web3dart for flutter makes a strange PrivateKey

I am using web3dart for a flutter application.
When we generate a private key like this

final rng = Random.secure();

    EthPrivateKey privateKey = EthPrivateKey.createRandom(rng);

Sometimes it returns a private key with two extra zeros:


If you wanted to use it in a Metamask you would have to remove those two zeros because it gives the following error:

Expected private key to be an Uint8Array with length 32

But if you remove those two zeros in the mobile application it does not work correctly.

What is the reason for these strange zeros?

At the same time I would like to ask if you know of a suitable flutter library.

The existing ones are abandoned and I can't find any that convince me apart from this web3dart.
It would be great to find one that uses ethers.js but the existing one has been abandoned. Do you know of alternatives?

There is a problem with the randomizer in web3Dart private key generation module. It generates 33 instead of a consistent 32 and this issue has been there for quite a while now.

This library ethers_dart is no longer maintained but it does the job of creating a proper private key and so i would recommend using it for generating wallet

import 'package:ether_dart/ether_dart.dart';

 Future<String> generateWallet() async {
 ///Create EtherDart without immediate connection
  final etherDart = EtherDart();

  ///Generate memomic phrase (can be called seed phrase (Eg : cow ram pig goat ))
  final memonicPhrase = etherDart.generateMemonicPhrase();

  ///Verify seed phrase
  if (memonicPhrase != null) {

    final wallet = etherDart.walletFromMemonicPhrase(memonicPhrase!);

    return wallet!.privateKey.toString();

Then you can have web3Dart handle the other interactions from the private key.

Thank you for your reply @Ayoseun.

I have tried that library because I wanted ethers.js and despite being abandoned I wanted to use it.
But it was not possible because of a conflict with my dependencies.

I have looked for another solution.
The use of this library:

Do you think it is a good solution?

Wha dependency is it conflicting with?
Did you mean bip39?

With ether_dart.dart.
No problems with bip39
If I remember correctly the conflict was with the http library.
I would have to look at it again.