GithubHelp home page GithubHelp logo

Comments (6)

TheMeanCanEHdian avatar TheMeanCanEHdian commented on May 18, 2024 5

Incredibly helpful. Thanks again!

from equatable.

felangel avatar felangel commented on May 18, 2024

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.

TheMeanCanEHdian avatar TheMeanCanEHdian commented on May 18, 2024

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.

felangel avatar felangel commented on May 18, 2024

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.

TheMeanCanEHdian avatar TheMeanCanEHdian commented on May 18, 2024

Interesting, thanks for your help!

So does Equatable not work on nested Maps?

from equatable.

felangel avatar felangel commented on May 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.