How can I resolve error "The argument type 'String' can’t be Assigned to the Parameter type 'List<int>' " - Flutter

Asked

Viewed 2,045 times

2

I’m trying to fetch an API Token using Flutter, but this code:

var _random = Random.secure();
var random = List<int>.generate(32, (i) => _random.nextInt(256));

var verificador = base64Url.encode(random).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');

var base64ToSha256 = sha256.convert(verificador);//erro aqui

var desafio = base64Url.encode(base64ToSha256).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');

But I’m having trouble using sha256.Convert

Error displayed: The argument type 'String' can’t be Assigned to the Parameter type 'List'

1 answer

2


I believe you’re using the lib crypto.

You are passing a string to the sha256:

var base64ToSha256 = sha256.convert(verificador);

But actually it accepts a list of bytes, which are represented in int. So it is necessary to do the Encode for utf8 (which will return a List<int>):

var base64ToSha256 = sha256.convert(utf8.encode(verificador));
  • Thanks Julio for the explanation. But now I have another problem ._.

  • The part: var desafio = base64Url.encode(base64ToSha256) is making that mistake The argument type 'Digest' can't be assigned to the parameter type 'List<int>' what serious method to fix? Gave a search on this error but did not find much (in fact found nothing)

  • What implementation/lib are you using for sha256?? It’s basically the same problem, you’re trying to pass a type Digest for a method that accepts an int List.

  • import 'package:Crypto/Crypto.Dart';

  • 1

    Okay, so the object Digest has an attribute bytes which is a list of int. Then do: var desafio = base64Url.encode(base64ToSha256.bytes)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.