FlutterFlow / flutterflow-issues

A community issue tracker for FlutterFlow.
122 stars 18 forks source link

Custom function Compile error: ... setter is not define on a custom DataType struct #90

Closed KirilOkun closed 1 year ago

KirilOkun commented 1 year ago

Issue tracker is ONLY used for reporting bugs. New feature suggestions and questions should be discussed on Community or submitted through our user feedback form.

Your issue may already be reported! Please search in the issue tracker before creating one.

Please thumbs up this issue if you have also experienced it. You may also add more information if there is something relevant that was not mentioned. However, please refrain from comments that are not constructive, like "I have this problem too", etc.

Expected behavior (required)

Since i saw that custom Data Types were available for custom functions i tried to use it as a return value that my function sets and returns. Code auto-complete recognized the struct and allowed to set its values (see code below) without any errors.

Current behavior (required)

But when trying to run the app i get the following errors for each attribute of the struct:

Launching lib/main.dart on Web Server in debug mode... Waiting for connection from debug service on Web Server...
lib/flutter_flow/custom_functions.dart:88:10: Error: The setter 'original' isn't defined for the class 'AmountStruct'.

To Reproduce (required)

Steps to reproduce the behavior:

  1. Create a custom function of any custom Data Type return type. AmountStruct in this example.
  2. In the function assign a value to its members like so: amount.original = someVariable;
  3. Run application in test mode
  4. Application errors out with the description above.

Custom function code:

import 'dart:math' as math;

AmountStruct addFee(String originalAmt) { double original = double.parse(originalAmt); double feePct = 15; double fee = original / 100 * feePct; double total = original + fee;

AmountStruct amount = new AmountStruct(); amount.original = original; amount.fee = fee; amount.total = total;

return amount; }

Context (required)

Using a defined custom Data Type would make things easier and clearer for passing the data around and saving to db. Have to hack around this now.

Screenshots / recordings

Your environment

bulgariamitko commented 1 year ago

I am not sure, but I am guessing those two are connected. https://github.com/FlutterFlow/flutterflow-issues/issues/20

KirilOkun commented 1 year ago

@bulgariamitko Hard to tell as his error is not the same as what i'm getting. It seems that even though the custom function does recognize the DataType struct it can't access the setters on it. Not sure if that's by design or not, but it would very convenient to do so.

KirilOkun commented 1 year ago

The workaround was to make the function type JSON and output a map ( Map result = { "key":"value", ...}. Then use this function as a source to populate a local state variable of type JSON. And finally its values can be used in other actions by accessing them with JSON path. A bit of a hassle but it works.

organicnz commented 1 year ago

@KirilOkun thanks for sharing your solution 👍

agreaves commented 1 year ago

Hi @KirilOkun! Thanks for the bug report. The correct way to create a DataType object in custom code is to use the function create[Data Type Name]Struct(...). I know this is confusing since there is no way to know this without looking in the downloaded code.

Trying to directly create and modify the fields of the DataType object will not work because in the final code the fields are final and not modifiable.

This is a completely valid bug report however, as we should not be validating the custom function if it modifies the fields. The good news is we are currently revamping our custom code analysis, so this (and a few other issues) will be fixed in the near future. I'll leave this issue open for now.

KirilOkun commented 1 year ago

@agreaves thanks for the follow up. the errors make sense now. on a related note do you guys have dart.doc documentation of the ff code somewhere for the curios minds?

agreaves commented 1 year ago

No, we do not at the moment actually, but that is an excellent idea! It would be great to have a small doc on common patterns in FlutterFlow code.

Closing this for now.

sirian-m commented 1 year ago

Hi @KirilOkun! Thanks for the bug report. The correct way to create a DataType object in custom code is to use the function create[Data Type Name]Struct(...). I know this is confusing since there is no way to know this without looking in the downloaded code.

Trying to directly create and modify the fields of the DataType object will not work because in the final code the fields are final and not modifiable.

This is a completely valid bug report however, as we should not be validating the custom function if it modifies the fields. The good news is we are currently revamping our custom code analysis, so this (and a few other issues) will be fixed in the near future. I'll leave this issue open for now.

I tried this in a custom function like this: categoryMap.forEach((key, value) { categories.add(createGiftCategoryStruct(displayName: key, iconFilename: value)); });

Still gives me the "This funtion 'createGiftCategoryStruct' isn't defined" error when compiling. The code analyzer doesn't have any issues with it tho and recognizes the function like it should.

@agreaves Does this issue need to be opened again or should data types be created in a different way in custom functions?

trevorFields commented 1 year ago

I would also like to raise this issue. when using custom functions with datatypes the create[datatype] method and any other methods such as ".rebuild" are verified by the code analyzer, and use gitHub and run on local emulator for testing and everything works fine.

BUT when running "Check Errors" it says those methods are not available.

follow up question is since i don't use the built in "Test Mode" and it works fine locally will i have issues if trying to use CodeMagic for final build and deploy?

or, is there an update coming that fixes the issue with "Check Errors" not recognizing datatype methods?

in Actions all methods work but many times you want to use datatypes in Functions not Actions.