0

I am linking cloud function to my Flutter model. Result is coming from Cloud-Function

  print(result.data);
  print(result.data.runtimeType);
  GameModel _incomingGame = GameModel.fromJson(result.data);

print(result.data);

{
    game: 'GTA',
    played: ['ISODATE', ...]
}

print(result.data.runtimeType);

flutter: _InternalLinkedHashMap<String, dynamic>

GameModel

@JsonSerializable()
class GameModel {
  String? game;
  List? played;

  GameModel(
      this.game,
      this.played,);

  factory GameModel.fromJson(Map<String, dynamic> json) =>
      _$GameModelFromJson(json);

  Map<String, dynamic> toJson() => _$GameModelToJson(this);
}

game_model.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'game_model.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

GameModel _$GameModelFromJson(Map<String, dynamic> json) => GameModel(
      json['game'] as String?,
      json['played'] as List?,
    );

Map<String, dynamic> _$GameModelToJson(GameModel instance) => <String, dynamic>{
      'game': instance.game,
      'played': instance.played,
    };

The incoming data result's runtimeType shows that it is Map<String, dynamic> in the console. However, when I execute GameModel.fromJson(), it causes type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast.

I really don't get that why this is happening somehow? Even if I do something like below also causes the same type cast error.

GameModel _gameData = result.data;
var game = GameModel.fromJson(_gameData);

Is there any way that I can fix this?

3
  • 1
    do you mind share GameModel class file too? Commented Dec 6, 2021 at 5:11
  • 1
    you can try with jsonDecode, like GameModel.fromJson(jsonDecode(result.data)); Commented Dec 6, 2021 at 5:18
  • jsonDecode(result.data) returns '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' error. Commented Dec 6, 2021 at 5:45

2 Answers 2

2

Try this solution:

GameModel.fromJson((result.data as Map<dynamic, dynamic>).cast<String, dynamic>())
Sign up to request clarification or add additional context in comments.

Comments

1

Try these solutions

  GameModel _incomingGame = GameModel.fromJson(result.data as Map<String, dynamic>);

Or

  GameModel _incomingGame = GameModel.fromJson(Map<String, dynamic>.from(result.data));

It also would be better if you check the types before executing type casts


If above solutions doesn't work, add the type when calling cloud functions

final func = FirebaseFunctions.instance.httpsCallable('gameFunction');
final result = await func<Map<String, dynamic>?>();

7 Comments

Having same problem :/ Map<String, dynamic> game = Map<String, dynamic>.from(result.data); and print(game.runtimeType); return Map<String, dynamic> but as soon as it goes into .fromJson(), it turns to Map<Object?, Object?>
Could you write the fromJson method manually? or add the generated method to question @chichi
Yeah that's odd, I updated the answer if you are using firebase cloud functions maybe this one works out, @chichi
Same stuffs. I am just restructuring result.data and just use GameModel(); to add directly to my variable. If I figure it out then I will keep ya update what was the problem bro. Thanks so much for help
There was one more object - map added by mongoose that coming with data as Map<Object?, Object?>... So, I've added an object to my MongoDB and all these objects are type casted as Map<Object?, Object?>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.