Comments (4)
You have missed specifying the type with the factory constructor.
It should be
@superEnum
enum _ModelState {
@generic
@Data(fields: [DataField('data', Generic)])
Success,
@object
Error,
@object
Loading,
@object
Empty,
}
//Somewhere else in code
Stream<ModelState<int>> getSomething() {
return Stream.fromFuture(Future.value(1))
.publishReplay(maxSize: 1)
.refCount()
.map((data) => ModelState<int>.loading());
}
Dart is unable to automatically infer template types.
As for the values added by enums are brevity of the code, simplicity and cohesion with the idea of sealed classes being superlative enums.
If dart had support for nested classes we could have had used classes but as per your example of sealed_unions, you have to rely on member methods to generate corresponding classes and factories. Neither it's readable nor does it make much sense.
from super_enum.
Seems like generics could be more easily handled using classes rather than enums. What's the value added by using enums?
from super_enum.
@astralstriker makes sense, thanks for pointing out that fix! That appeared to solve my initial issue, however I encounter the same error when using startWith
Stream<ModelState<int>> getSomething() {
return Stream.fromFuture(Future.value(1))
.publishReplay(maxSize: 1)
.refCount()
.map((data) => ModelState<int>.success(data: data))
.startWith(ModelState<int>.loading());
It shouldn't matter, but I am using a more complex type and seeing the same error when I consume the observable.
return Stream.fromFuture(Future.value())
.publishReplay(maxSize: 1)
.refCount()
.map((data) => ModelState<VehicleTypeResults>.loading());
This appears to result in the same error. The only call that has worked correctly for me is success.
from super_enum.
I believe the issue is in how non-generic classes are created with singletons. Notice the following screenshot that shows the _instance
is dynamic.
If I swap the generated code for Loading with similar code as Success the issue is resolved.
class Loading<T> extends ModelState<T> {
const Loading._() : super(_ModelState.Loading);
factory Loading() {
_instance ??= Loading._();
return _instance;
}
static Loading _instance;
}
//Becomes
class Loading<T> extends ModelState<T> {
const Loading() : super(_ModelState.Loading);
}
From what I understand, static members also cannot be referenced with generic types. See dart-lang/language#359
from super_enum.
Related Issues (20)
- Enum as data field in another enum HOT 1
- DataFields with a name including 'T' break code generation HOT 1
- use const constructor
- @UseClass generating Error HOT 1
- Generate copyWith methods for data classes
- Create a separate whenAsync method for handling asynchronous calls and remove FutureOr from whenXXX.
- Linter Error with super_enum_generator 0.4.2 HOT 8
- Generate whenPartial with normal return type HOT 2
- Why renaming enum fields? HOT 2
- Optional fields HOT 5
- emit `when` method arguments for "empty" actions HOT 2
- Use PartBuilder instead of SharedPartBuilder in generator
- Allow setting null value in copyWith function
- WhenPartial breaks when updraging from 0.3.0 to 0.4.2 HOT 2
- Can you please make a release? HOT 1
- Could we allow to generate some types as private types to be consumed by the bloc itself only?
- super_enum is not working with analyzer ^0.40.1 HOT 2
- Overriding == operator of Data classes
- How can I convert a String to a Enum Type
- Release null safe version ? HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from super_enum.