I'm developing a flutter app using Grpc to connect to the server. Some of the services need extra metadata for authentication, so the first thing that comes to my mind is implementing an interceptor to add the metadata to those requests like this:
class MyClientInterceptor implements ClientInterceptor {
@override
ResponseFuture<R> interceptUnary<Q, R>(ClientMethod<Q, R> method, Q request, CallOptions options, invoker) {
var newOptions = CallOptions.from([options])
..metadata.putIfAbsent('token', () => 'Some-Token');
return invoker(method, request, newOptions);
}
}
But I get Caught error: Unsupported operation: Cannot modify unmodifiable map
because CallOptions uses an unmodifiable map.
First question: What is the best practice to add authentication to some of the requests instead of creating the Client stub with those metadata?
Second: How can I copy the metadata from options, modify it and use the modified object?