案例:将数据模型中的16进制颜色字符串转化为Color
import 'package:flutter/material.dart';
class CategoryModel {
String id;
String title;
String color;
Color hexColor;
CategoryModel({this.id, this.title, this.color});
CategoryModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
color = json['color'];
hexColor = Color(int.parse(color,radix: 16) | 0xFF000000);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['color'] = this.color;
return data;
}
}
使用:
网友评论