Back

[SOLVED] Error with "env" getter

  • 0
  • Flutter
  • Functions
Disk_MTH
28 Sep, 2023, 20:21

Hello, I have an error when I try to access "context.env", the compiler tells me that this attribute does not exist while all your docs say that this is how it should be done. Any idea of the cause?

TypeScript
  final backend = Client()
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject(context.env.APPWRITE_FUNCTION_PROJECT_ID)
      .setKey(context.env.APPWRITE_API_KEY);
TypeScript
NoSuchMethodError: Class 'RuntimeContext' has no instance getter 'env'.
Receiver: Instance of 'RuntimeContext'
Tried calling: env
#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:13)
#3      main.<anonymous closure>.<anonymous closure> (file:///usr/local/server/src/server.dart:115)
#4      main.<anonymous closure>.<anonymous closure> (file:///usr/local/server/src/server.dart:111)
#5      _rootRun (dart:async/zone.dart:1426)
#6      _CustomZone.run (dart:async/zone.dart:1328)
#7      _runZoned (dart:async/zone.dart:1861)
#8      runZoned (dart:async/zone.dart:1783)
#9      main.<anonymous closure> (file:///usr/local/server/src/server.dart:110)
<asynchronous suspension>
#10     handleRequest (package:shelf/shelf_io.dart:138)
<asynchronous suspension>
TL;DR
User is experiencing an error with accessing "context.env" in Dart. Compiler says that the attribute does not exist. The solution is to access environment variables using `Platform.environment` instead of `context.env`. The correct code would be: ```dart final backend = Client() .setEndpoint("https://cloud.appwrite.io/v1") .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID']) .setKey(Platform.environment['APPWRITE_API_KEY']); ``` This should resolve the error.
Drake
28 Sep, 2023, 20:25

There's no such thing as context.env. in dart, you access environment variables via Platform.environment. see https://api.dart.dev/stable/3.1.2/dart-io/Platform-class.html

Disk_MTH
28 Sep, 2023, 20:30

ok thanks i will look that

Disk_MTH
5 Oct, 2023, 17:30

[SOLVED] Error with "env" getter

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more