本文主要是介绍dart 中的 泛型 怎么写??,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import 'package:flutterdemo/bean/MyUser.dart';class BaseResp<T> {int code;String str;T data;BaseResp({this.code, this.str, this.data});BaseResp.fromJson(Map<String, dynamic> jsonStr) {code = jsonStr['code'];str = jsonStr['str'];///关键部分if (T == MyUser) {data = MyUser.fromJson(jsonStr['data']) as T;} else {data = jsonStr['data'];}}Map<String, dynamic> toJson() {final Map<String, dynamic> data = new Map<String, dynamic>();data['code'] = this.code;data['str'] = this.str;///关键部分if (this.data == MyUser) {data['data'] = (this.data as MyUser).toJson();} else {data['data'] = this.data;}return data;}
}
class MyUser {int userId;String realName;String u
这篇关于dart 中的 泛型 怎么写??的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!