Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading enum from ConstantReader #3

Open
Huholoman opened this issue Oct 24, 2023 · 1 comment
Open

reading enum from ConstantReader #3

Huholoman opened this issue Oct 24, 2023 · 1 comment
Assignees
Labels
usage How to use the library

Comments

@Huholoman
Copy link

Hello,
Im trying to get enum from ConstantReader. Ive found your library and tried to use it.

I have annotation with StrategyEnum? strategy property. So In my code where Im trying to read the strategy property I tried this

final strategy = constantReader.get<StrategyEnum?>();

Unfortunately it threw error: ErrorOf<ConstantReader>: "Input does not represent an object of type <StrategyEnum?>"

I think I may be using your generic_reader incorrectly as I would expect get method to accept property name 🤔

Thanks in advance!

@simphotonics
Copy link
Owner

simphotonics commented Oct 26, 2023

The extension GenericReader can handle Dart enums and in the following I am assuming that StrategyEnum is a Dart enum. However, StrategyEnum? is a completely different type that is not known to GenericReader, unless you define a custom Decoder.

You could either:

  1. define a Decoder that can handle StrategyEnum?:
 // Adding a decoder for constants of type StrategyEnum?.
  GenericReader.addDecoder<StrategyEnum?>((constantReader) {
    if (constantReader.isNull) { 
       return null;
    }else{
       return constantReader.get<StrategyEnum>();
    }
 });   
  1. If it is your code, I would recommend refactoring the variable strategy by making it non nullable. For example, you could use Strategy.none to represent the absence of a strategy instead of null.
    Then you can use:
final strategy = constantReader.get<StrategyEnum>();

@simphotonics simphotonics self-assigned this Oct 26, 2023
@simphotonics simphotonics added the usage How to use the library label Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
usage How to use the library
Projects
None yet
Development

No branches or pull requests

2 participants