Back

[SOLVED] What would be Dart data type for a String Array attribute in a database collection ?

  • 0
  • Databases
  • Functions
punti_z
4 Apr, 2023, 23:33

I tried List<String> but function errors out

"Type '_GrowableList<dynamic> is not a subtype of 'List<String>'

Anyone can share a quick sample of a dart cloud function to read from and write to an string array attribute in a collection.

TL;DR
Solution: The Dart data type for a String Array attribute in a database collection would be `List<String>`. If you encounter an error like "Type '_GrowableList<dynamic> is not a subtype of 'List<String>'," you can try using `value.cast<String>();` or defaulting to an empty list if the value is null. Here's an example of how to read from and write to a string array attribute in a collection using a Dart cloud function: To read from a string array attribute: ```dart List<String> x = document.data['array_attribute'].cast<String>(); ``` To write to a string array
Drake
4 Apr, 2023, 23:39

you can do value.cast<String>(); but it might be better to be more defensive and check if value were null and default to an empty list.

punti_z
4 Apr, 2023, 23:50

Sorry but I would need more then that. I currently getDocument and then

List<String> x = received document.data['array_attribute']

So what do I change ?

Apologies for dumbing it down

Binyamin
5 Apr, 2023, 00:50

Like so

TypeScript
  List<dynamic> dList = ['First string', 'And another'];

  List<String> stringList = dynamiclist.cast<String>();
Binyamin
5 Apr, 2023, 00:51

So in your example you can do

TypeScript
  List<String> data =  document.data['array_attribute'].cast<String>(); 
Tessa
7 Apr, 2023, 21:18

@punti_z did you get your question resolved or still needed help? checking to see if we should close or not 🙂

punti_z
7 Apr, 2023, 21:40

[Closed] What would be Dart data type for a String Array attribute in a database collection ?

punti_z
7 Apr, 2023, 21:40

Done

Tessa
7 Apr, 2023, 21:40

[SOLVED] What would be Dart data type for a String Array attribute in a database collection ?

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