VeryGoodOpenSource/very_good_analysis

how to avoid_dynamic_calls from parsing json to object

riloraspopo opened this issue · 1 comments

i always got 'avoid_dynamic_calls' when i try to parsing json to my class object

example usermodel.dart

factory usermodel.fromJson(Map<String, dynamic> json){
    return new usermodel(
      id: json['id'], //'avoid_dynamic_calls' 
      userId: json['userId'], //'avoid_dynamic_calls' 
      name: json['name'], //'avoid_dynamic_calls' 
    );
  }

how escape from rule 'avoid_dynamic_calls' thanks...

Hi @riloraspopo 👋
Thanks for opening an issue!

You should be able to do something like:

class User {
  const User({required this.id, required this.name});

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] as String,
      name: json['name'] as String,
    );
  }

  final String id;
  final String name;
}

It might be worthwhile investing in something like json_serializable to handle this for you because you'll likely want to handle checking for null values etc.

Hope that helps 👍