The problem that is occurring may be related to the expression I pointed out in the comments. That is, the following expression is inadequately written:
FishNotifier fishNotifier = Provider.of(context)<FishNotifier>(context, listen: false);
The type given to the static method Provider::of<T>
(that is, that T
) serves to identify what type of data provider is being obtained. However, the correct way to pass this type is:
FishNotifier fishNotifier = Provider.of<FishNotifier>(context, listen: false);
In the same way is the notation I put up, making it even unnecessary the other (context)
.
Why does this mistake happen?
The error itself that was generated is not so much related to a possible syntax error, or something like that, but rather, the an assertion made in the package/library provider
.
That assertion defines the type provided to the method Provider::of<T>
shouldn’t be dynamic
. But maybe you say:
But I didn’t pass dynamic
! What I tried to go through was FishNotifier
!
Well done. When a class, method or function that uses Generics - that is, a class/method/function that asks for a type that will be internally used to define variables or methods (these, in the case of classes), avoiding duplication of code - does not receive this type that it is asking for or waiting for, in Dart, the past type is considered to have been dynamic
.
That is, in Provider.of(context)
, as "apparently" was not provided the type that the method asks for, so it was provided the type dynamic
, which is exactly one type that the method said should not be there! Here’s an example of this situation:
class Sequence<T> {
final Iterable<T> _iter;
Sequence(this._iter) : assert(T != dynamic);
String toString() {
return Set<T>.from(_iter).toString();
}
}
void main() {
// Roda normalmente
Sequence<int> sequence1 = new Sequence<int>([1, 2, 3]);
print(sequence1);
// Se as asserções estiverem ativadas [1], gera uma exceção
Sequence sequence2 = new Sequence([1, 2, 3]);
print(sequence2);
}
I define a class called Sequence
who uses Generics. In this case, she uses the type that is identified by the generic T
to create a variable that holds an iterable (which can be a List
or a Set
, for example) returning elements of this type T
. But that kind of T
shouldn’t be dynamic
, as defined in the command assert
.
In function main
, are two examples. In the first example, I created an object Sequence
with the guy int
. That is, the eternal to be saved must return elements of the type int
, which exactly matches the list passed as argument ([1, 2, 3]
).
Already in the second example, as I did not pass the type, it will provide the type dynamic
, which, in the class itself, I had defined should not be a possible value for T
. Thus, it will generate an interruption[1].
[1] - In Flutter, the commands assert
only work in debug mode (debug). When it comes to Dart only, the operation of the command assert
will depend on some things you can see in that reply.
I hope I’ve helped!
I think there, in
Provider.of(context)<FishNotifier>
, the indication of the type should come before, no? That way:Provider.of<FishNotifier>(context)
. See the documentation:Provider::of
.– Gustavo Sampaio