How to work with json data read from database with RPC package
I am trying to implement an API using RPC RPC package.
In all the example I've found, the response is manually generated (ie a new Response () .. message = "hello").
In my case, I am reading JSON data from mongodb and want to return it with minimal conversion (basically fetching only external properties).
To do this, you can use fromRequest
the method diagrams:
class QueryResult {
//my props
}
@ApiMethod(path: "myMongoQuery")
Future<List<QueryResult>> myMongoQuery() async {
var schema = _server.apiMap["/query/v1"].schemaMap["QueryResult"];
var results = await coll.find();
return results.map(schema.fromRequest).toList();
}
The problem in my code is the first line ( _server.apiMap["/query/v1"].schemaMap["QueryResult"]
), which is a clean hack to extract the schema of my method.
I tried using a mirror to fetch the schema in an elegant / generic way, but it didn't work.
Can anyone help me?
Greetings,
Nicholas
source to share
No one has answered this question yet
Check out similar questions: