Passing class static factory as method parameter in Dart

独自空忆成欢 提交于 2021-02-16 06:43:07

问题


I'm currently attempting to abstract making different HTTP requests by using generics. I'm using json_serializale for generating fromJson() and toJson() methods.

Here is a simple model file:

import 'package:json_annotation/json_annotation.dart';

part 'article.g.dart';

@JsonSerializable()
class Article {
  int id = 0;
  String title = "";

  Article({this.id, this.title});

  factory Article.fromJson(Map<String, dynamic> json) =>
      _$ArticleFromJson(json);
  Map<String, dynamic> toJson() => _$ArticleToJson(this);
}

I have a generic class that needs to be passed the fromJson-method, see:

typedef CreateModelFromJson = dynamic Function(Map<String, dynamic> json);

class HttpGet<Model> {
  CreateModelFromJson createModelFromJson;

  HttpGet({
    this.createModelFromJson,
  });

  Future<Model> do() async {
    // [... make HTTP request and do stuff ...]
    return createModelFromJson(jsonData);
  }
}

And finally, here is the ArticleService:

class ArticleService {
  Future<Model> read() => HttpGet<Article>({
    createModelFromJson: Article.fromJson,
  }).do();
}

This underlines-in-red fromJson in createModelFromJson: Article.fromJson,, with the error being:

The getter 'fromJson' isn't defined for the class 'Article'. Try importing the library that defines 'fromJson', correcting the name to the name of an existing getter, or defining a getter or field named 'fromJson'.dart(undefined_getter)

Obviously the compiler believes .fromJson to be a static field. However, it's, as can be seen above, a static factory method.

1.) Can you help with how to pass the constructor to the generic class?

ALSO:
2.) I'm not sure whether I'll actually get an Article back, or if I have to type cast it first? I'm kind of worried about the dynamic return type of the typedef.

3.) I'm also open for better ideas for what I'm attempting to do.


回答1:


Dart currently does not allow using constructors as tear-offs. I recommend using a static method instead.

Alternatively you could just create an explicit closure:

class ArticleService {
  Future<Model> read() => HttpGet<Article>({
    createModelFromJson: (json) => Article.fromJson(json),
  }).do();
}


来源:https://stackoverflow.com/questions/55962139/passing-class-static-factory-as-method-parameter-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!