Back
NoSuchMethodError: Class 'RuntimeResponse' has no instance method 'json'.
- 0
- Functions
- Web
- Cloud
Vincent Berthoumieux
Hello,
I'm calling a dart function from a web browser but I can't send a response back, I have this error:
TypeScript
NoSuchMethodError: Class 'RuntimeResponse' has no instance method 'json'.
Receiver: Instance of 'RuntimeResponse'
Tried calling: json(_Map len:0, headers: _ConstMap len:3)
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38)
#1 _objectNoSuchMethod (dart:core-patch/object_patch.dart:85)
#2 main (package:starter_template/main.dart:24)
#3 action.<anonymous closure> (file:///usr/local/server/src/server.dart:116)
#4 _rootRun (dart:async/zone.dart:1399)
#5 _CustomZone.run (dart:async/zone.dart:1301)
#6 _runZoned (dart:async/zone.dart:1804)
#7 runZoned (dart:async/zone.dart:1747)
#8 action (file:///usr/local/server/src/server.dart:111)
<asynchronous suspension>
#9 main.<anonymous closure> (file:///usr/local/server/src/server.dart:185)
<asynchronous suspension>
#10 handleRequest (package:shelf/shelf_io.dart:138)
<asynchronous suspension>
The query seems to be a json type (cf picture) as mentioned on https://appwrite.io/docs/products/functions/develop#response-types
My function is this:
TypeScript
const corsHeaders = {
'Access-Control-Allow-Origin': 'berthou.github.io',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
Future<dynamic> main(final context) async {
...
if (context.req.method == 'OPTIONS') {
return context.res.json(// --------------- EXCEPTION HERE ----------
{},
headers: corsHeaders,
);
}
final request = {
'api_key': octopusApiKey,
'email_address': emailaddress,
};
final response = await http.post(...);
if (response.statusCode == 200) {
return context.res.json(
{},
headers: corsHeaders,
);
} else {
return context.res.json(
{},
headers: corsHeaders,
);
}
}
What am I missing?
TL;DR
Developers are encountering a NoSuchMethodError when trying to use the 'json' method in a response in their Dart function. The error occurs because 'json' method does not exist for 'RuntimeResponse'. To resolve this, they should return a response in JSON format using the 'jsonEncode()' method from the 'dart:convert' package:
```dart
import 'dart:convert';
...
if (context.req.method == 'OPTIONS') {
return context.res
..headers.addAll(corsHeaders)
..write(jsonEncode({}));
}
final request = {
'api_key': octopusApiKey,
'email_address': emailaddress,
Recommended threads
- Need help with createExecution function
Hi, Need some help understanding createExecution. When requesting function execution via createExecution, the function handler arguments are incorrect and rese...
- Query Appwrite
Hello, I have a question regarding Queries in Appwrite. If I have a string "YYYY-MM", how can I query the $createdAt column to match this filter?
- Need Help with Google OAuth2 in Expo usi...
I'm learning React Native with Expo and trying to set up Google OAuth2 with Appwrite. I couldn't find any good docs or tutorials for this and my own attempt did...