Comments (6)
Incredibly helpful. Thanks again!
from equatable.
Hi @TheMeanCanEHdian 👋
Thanks for opening an issue!
I believe the issue you aren't specifying the type of the Map
.
Can you try the following and let me know if that helps?
class Activity extends Equatable {
final String result;
final String message;
final Map<String, dynamic> data;
Activity({
this.data,
this.message,
this.result
});
@override
List<Object> get props => [data, message, result];
@override
bool get stringify => true;
}
from equatable.
Thank you for the quick response. I applied the type where you suggested and to the ActivityModel data map (only applying it to Activity resulted in an error). But I am still getting the same error response from the test.
from equatable.
Hi @TheMeanCanEHdian 👋
Sorry for the delay I finally had some time and tweaked your example to work properly:
import 'package:equatable/equatable.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meta/meta.dart';
class ActivityData extends Equatable {
final int streamCount;
final List<String> sessions;
ActivityData({@required this.streamCount, @required this.sessions});
factory ActivityData.fromJson(Map<String, dynamic> json) {
return ActivityData(
streamCount: int.tryParse(json['stream_count']),
sessions: (json['sessions'] as List).map((e) => e.toString()).toList(),
);
}
@override
List<Object> get props => [streamCount, sessions];
}
class Activity extends Equatable {
final String result;
final String message;
final ActivityData data;
Activity({this.data, this.message, this.result});
@override
List<Object> get props => [data, message, result];
@override
bool get stringify => true;
}
class ActivityModel extends Activity {
ActivityModel({
@required ActivityData data,
@required String message,
@required String result,
}) : super(data: data, message: message, result: result);
factory ActivityModel.fromJson(Map<String, dynamic> json) {
return ActivityModel(
data: ActivityData.fromJson(json['response']['data']),
message: json['response']['message'],
result: json['response']['result'],
);
}
Map<String, dynamic> toJson() {
return {
'result': result,
'message': message,
'data': data,
};
}
}
void main() {
final tActivityModel = ActivityModel(
result: 'success',
message: null,
data: ActivityData(
streamCount: 1,
sessions: [],
),
);
group('fromJson', () {
test(
'should return a valid model',
() async {
// arrange
final Map<String, dynamic> jsonMap = {
"response": {
"result": "success",
"message": null,
"data": {
"stream_count": "1",
"sessions": [],
}
}
};
//act
final result = ActivityModel.fromJson(jsonMap);
//assert
expect(result, tActivityModel);
},
);
});
}
Hope that helps 👍
from equatable.
Interesting, thanks for your help!
So does Equatable not work on nested Maps?
from equatable.
@TheMeanCanEHdian no because nested maps aren't Equatable.
Take the following example:
void main() {
final a = {
'section_number': '1',
'sessions': [],
};
final b = {
'section_number': '1',
'sessions': [],
};
print(a == b); // false
}
from equatable.
Related Issues (20)
- Same hashcode for different new lists HOT 1
- Equality and Hashcode differ for Sets with different order
- Equatable doesn't checks for Equality for variables in parent class HOT 3
- feat request: add `stringifyProps` HOT 4
- Enum implementing Equatable related classes HOT 3
- Same object, same runtimeType but runtimeType== other.runtimeType is false HOT 3
- Storing the hashCode HOT 3
- Doesn't work with List<File> as property HOT 1
- `runtimeType` prevents from using generic type HOT 5
- Provide lint for forgotten fields in props HOT 3
- List of object dosen't work correctly HOT 12
- `Equatable` is redundant with `EquatableMixin` HOT 1
- Upgrade sdk to include Dart 3 HOT 3
- A Map considered equal even if its not HOT 3
- Why do we need to compare runtimeType? HOT 5
- Support for mutable class objects HOT 3
- `Foo(1) == Foo(1.0)` is false HOT 2
- Do Equatable actually need to be immutable HOT 1
- Publish a new version HOT 2
- Is that logical? Object equals but element not equals. HOT 1
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 equatable.