How to return this crazy type in Dart?


I am trying to mock the .find function in mongo_dart and it is asking for a return function that I just can’t work out. It has a type, then another type, then the name. What / how do I express this return type.

This is legitimate code apparently:

Stream<Map<String, dynamic>> Function([selector]) _test(Invocation realInvocation) {

class selector {

Returning a Stream<Map<String, dynamic>> throws an error – it says it needs a Stream<Map<String, dynamic>> Function([selector])

Help! I have never seen anything like it and Google isn’t much help.

Edit, thanks to Irn’s comment below, the solution ultimately was this code. See their answer and the comment I made for clarification

Stream<Map<String, dynamic>> Function([dynamic]) _test(Invocation realInvocation) {
  return ([selector]) {
    return Stream.value({"response":true});


The return type is Stream<Map<String, dynamic>> Function([selector]).
That’s the type of a function. A function declaration with that type could be:

Stream<Map<String, dynamic>> createMaps([selector s]) async* {
  ... do something ...
    yield map;
  ... do more ...

The [selector] part means that the function must accept one optional positional parameter with the type selector (which really should be capitalized to make it clear that it’s a type).
The Stream<Map<String, dynamic>> means that the function returns a stream.

So, since _test returns such a function, you could write test as:

Stream<Map<String, dynamic>> Function([selector]) _test(Invocation realInvocation) {
  return createMaps; // But should probably use realInvocation for something.

(That would make the type of _test be:

Stream<Map<String, dynamic>> Function([selector]) Function(Invocation)

which is a mouthful!)

Answered By – lrn

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.