如何在 hive 中添加 json 数据?

x4shl7ld  于 2021-04-03  发布在  Hive
关注(0)|答案(1)|浏览(569)

我是一个新来的,我没有找到任何文章或视频来介绍如何在hive中使用model类添加json数据,请帮助我。
json数据链接
https://drive.google.com/file/d/1k1un0vd-snxgcrqyhy7bckznicjuptdk/view?usp=sharing

64jmpszr

64jmpszr1#

首先,你需要从这里将你的json数据转换为dart模型,你的模型会像这样。

class YourModel {
  Pagination pagination;
  List<Results> results;

  YourModel({this.pagination, this.results});

  YourModel.fromJson(Map<String, dynamic> json) {
    pagination = json['pagination'] != null
        ? new Pagination.fromJson(json['pagination'])
        : null;
    if (json['results'] != null) {
      results = new List<Results>();
      json['results'].forEach((v) {
        results.add(new Results.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.pagination != null) {
      data['pagination'] = this.pagination.toJson();
    }
    if (this.results != null) {
      data['results'] = this.results.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Pagination {
  int offset;
  int limit;
  Null total;

  Pagination({this.offset, this.limit, this.total});

  Pagination.fromJson(Map<String, dynamic> json) {
    offset = json['offset'];
    limit = json['limit'];
    total = json['total'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['offset'] = this.offset;
    data['limit'] = this.limit;
    data['total'] = this.total;
    return data;
  }
}

class Results {
  String id;
  String type;
  Null title;
  String text;
  String anchor;
  User user;
  Null annotation;
  Null createTime;
  String modifiedTime;
  Null language;
  Null commentsId;
  List<Attachments> attachments;
  Null tags;
  ReferenceInfo referenceInfo;
  Feedback feedback;

  Results(
      {this.id,
      this.type,
      this.title,
      this.text,
      this.anchor,
      this.user,
      this.annotation,
      this.createTime,
      this.modifiedTime,
      this.language,
      this.commentsId,
      this.attachments,
      this.tags,
      this.referenceInfo,
      this.feedback});

  Results.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    type = json['type'];
    title = json['title'];
    text = json['text'];
    anchor = json['anchor'];
    user = json['user'] != null ? new User.fromJson(json['user']) : null;
    annotation = json['annotation'];
    createTime = json['createTime'];
    modifiedTime = json['modifiedTime'];
    language = json['language'];
    commentsId = json['commentsId'];
    if (json['attachments'] != null) {
      attachments = new List<Attachments>();
      json['attachments'].forEach((v) {
        attachments.add(new Attachments.fromJson(v));
      });
    }
    tags = json['tags'];
    referenceInfo = json['referenceInfo'] != null
        ? new ReferenceInfo.fromJson(json['referenceInfo'])
        : null;
    feedback = json['feedback'] != null
        ? new Feedback.fromJson(json['feedback'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['type'] = this.type;
    data['title'] = this.title;
    data['text'] = this.text;
    data['anchor'] = this.anchor;
    if (this.user != null) {
      data['user'] = this.user.toJson();
    }
    data['annotation'] = this.annotation;
    data['createTime'] = this.createTime;
    data['modifiedTime'] = this.modifiedTime;
    data['language'] = this.language;
    data['commentsId'] = this.commentsId;
    if (this.attachments != null) {
      data['attachments'] = this.attachments.map((v) => v.toJson()).toList();
    }
    data['tags'] = this.tags;
    if (this.referenceInfo != null) {
      data['referenceInfo'] = this.referenceInfo.toJson();
    }
    if (this.feedback != null) {
      data['feedback'] = this.feedback.toJson();
    }
    return data;
  }
}

class User {
  Null requestStatus;
  String id;
  String emailAddress;
  Null description;
  Null links;
  Null credentialsNonExpired;
  Null status;
  String authProvider;
  bool isVerified;
  Null fileId;
  String roles;
  UserDetails userDetails;
  String fcmToken;
  bool connected;
  bool following;
  BusinessDetails businessDetails;

  User(
      {this.requestStatus,
      this.id,
      this.emailAddress,
      this.description,
      this.links,
      this.credentialsNonExpired,
      this.status,
      this.authProvider,
      this.isVerified,
      this.fileId,
      this.roles,
      this.userDetails,
      this.fcmToken,
      this.connected,
      this.following,
      this.businessDetails});

  User.fromJson(Map<String, dynamic> json) {
    requestStatus = json['requestStatus'];
    id = json['id'];
    emailAddress = json['emailAddress'];
    description = json['description'];
    links = json['links'];
    credentialsNonExpired = json['credentialsNonExpired'];
    status = json['status'];
    authProvider = json['authProvider'];
    isVerified = json['isVerified'];
    fileId = json['fileId'];
    roles = json['roles'];
    userDetails = json['userDetails'] != null
        ? new UserDetails.fromJson(json['userDetails'])
        : null;
    fcmToken = json['fcmToken'];
    connected = json['connected'];
    following = json['following'];
    businessDetails = json['businessDetails'] != null
        ? new BusinessDetails.fromJson(json['businessDetails'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['requestStatus'] = this.requestStatus;
    data['id'] = this.id;
    data['emailAddress'] = this.emailAddress;
    data['description'] = this.description;
    data['links'] = this.links;
    data['credentialsNonExpired'] = this.credentialsNonExpired;
    data['status'] = this.status;
    data['authProvider'] = this.authProvider;
    data['isVerified'] = this.isVerified;
    data['fileId'] = this.fileId;
    data['roles'] = this.roles;
    if (this.userDetails != null) {
      data['userDetails'] = this.userDetails.toJson();
    }
    data['fcmToken'] = this.fcmToken;
    data['connected'] = this.connected;
    data['following'] = this.following;
    if (this.businessDetails != null) {
      data['businessDetails'] = this.businessDetails.toJson();
    }
    return data;
  }
}

class UserDetails {
  String username;
  String firstName;
  Null lastName;
  Null middleName;
  String contactNo;
  String location;
  Null credentialsNonExpired;
  Null status;
  Null imageURL;
  String gender;
  Null roles;
  Null portFolioDetails;
  List<String> industries;
  List<String> expertise;
  List<String> offering;
  Interest interest;

  UserDetails(
      {this.username,
      this.firstName,
      this.lastName,
      this.middleName,
      this.contactNo,
      this.location,
      this.credentialsNonExpired,
      this.status,
      this.imageURL,
      this.gender,
      this.roles,
      this.portFolioDetails,
      this.industries,
      this.expertise,
      this.offering,
      this.interest});

  UserDetails.fromJson(Map<String, dynamic> json) {
    username = json['username'];
    firstName = json['firstName'];
    lastName = json['lastName'];
    middleName = json['middleName'];
    contactNo = json['contactNo'];
    location = json['location'];
    credentialsNonExpired = json['credentialsNonExpired'];
    status = json['status'];
    imageURL = json['imageURL'];
    gender = json['gender'];
    roles = json['roles'];
    portFolioDetails = json['portFolioDetails'];
    industries = json['industries'].cast<String>();
    expertise = json['expertise'].cast<String>();
    offering = json['offering'].cast<String>();
    interest = json['interest'] != null
        ? new Interest.fromJson(json['interest'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['username'] = this.username;
    data['firstName'] = this.firstName;
    data['lastName'] = this.lastName;
    data['middleName'] = this.middleName;
    data['contactNo'] = this.contactNo;
    data['location'] = this.location;
    data['credentialsNonExpired'] = this.credentialsNonExpired;
    data['status'] = this.status;
    data['imageURL'] = this.imageURL;
    data['gender'] = this.gender;
    data['roles'] = this.roles;
    data['portFolioDetails'] = this.portFolioDetails;
    data['industries'] = this.industries;
    data['expertise'] = this.expertise;
    data['offering'] = this.offering;
    if (this.interest != null) {
      data['interest'] = this.interest.toJson();
    }
    return data;
  }
}

class Interest {
  List<String> professional;
  List<String> collab;

  Interest({this.professional, this.collab});

  Interest.fromJson(Map<String, dynamic> json) {
    professional = json['professional'].cast<String>();
    collab = json['collab'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['professional'] = this.professional;
    data['collab'] = this.collab;
    return data;
  }
}

class BusinessDetails {
  Null legalName;
  Null docNumber;
  String username;
  List<String> industries;
  List<String> offering;
  String myIndustry;
  List<String> expertise;
  Null portFolioDetails;

  BusinessDetails(
      {this.legalName,
      this.docNumber,
      this.username,
      this.industries,
      this.offering,
      this.myIndustry,
      this.expertise,
      this.portFolioDetails});

  BusinessDetails.fromJson(Map<String, dynamic> json) {
    legalName = json['legalName'];
    docNumber = json['docNumber'];
    username = json['username'];
    industries = json['industries'].cast<String>();
    offering = json['offering'].cast<String>();
    myIndustry = json['myIndustry'];
    expertise = json['expertise'].cast<String>();
    portFolioDetails = json['portFolioDetails'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['legalName'] = this.legalName;
    data['docNumber'] = this.docNumber;
    data['username'] = this.username;
    data['industries'] = this.industries;
    data['offering'] = this.offering;
    data['myIndustry'] = this.myIndustry;
    data['expertise'] = this.expertise;
    data['portFolioDetails'] = this.portFolioDetails;
    return data;
  }
}

class Attachments {
  String fileId;
  Null fileName;

  Attachments({this.fileId, this.fileName});

  Attachments.fromJson(Map<String, dynamic> json) {
    fileId = json['fileId'];
    fileName = json['fileName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['fileId'] = this.fileId;
    data['fileName'] = this.fileName;
    return data;
  }
}

class ReferenceInfo {
  Post post;

  ReferenceInfo({this.post});

  ReferenceInfo.fromJson(Map<String, dynamic> json) {
    post = json['post'] != null ? new Post.fromJson(json['post']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.post != null) {
      data['post'] = this.post.toJson();
    }
    return data;
  }
}

class Post {
  String id;
  String type;
  Null title;
  String text;
  String anchor;
  User user;
  Null annotation;
  Null createTime;
  String modifiedTime;
  Null language;
  Null commentsId;
  List<Attachments> attachments;
  Null tags;
  Null referenceInfo;
  Feedback feedback;

  Post(
      {this.id,
      this.type,
      this.title,
      this.text,
      this.anchor,
      this.user,
      this.annotation,
      this.createTime,
      this.modifiedTime,
      this.language,
      this.commentsId,
      this.attachments,
      this.tags,
      this.referenceInfo,
      this.feedback});

  Post.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    type = json['type'];
    title = json['title'];
    text = json['text'];
    anchor = json['anchor'];
    user = json['user'] != null ? new User.fromJson(json['user']) : null;
    annotation = json['annotation'];
    createTime = json['createTime'];
    modifiedTime = json['modifiedTime'];
    language = json['language'];
    commentsId = json['commentsId'];
    if (json['attachments'] != null) {
      attachments = new List<Attachments>();
      json['attachments'].forEach((v) {
        attachments.add(new Attachments.fromJson(v));
      });
    }
    tags = json['tags'];
    referenceInfo = json['referenceInfo'];
    feedback = json['feedback'] != null
        ? new Feedback.fromJson(json['feedback'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['type'] = this.type;
    data['title'] = this.title;
    data['text'] = this.text;
    data['anchor'] = this.anchor;
    if (this.user != null) {
      data['user'] = this.user.toJson();
    }
    data['annotation'] = this.annotation;
    data['createTime'] = this.createTime;
    data['modifiedTime'] = this.modifiedTime;
    data['language'] = this.language;
    data['commentsId'] = this.commentsId;
    if (this.attachments != null) {
      data['attachments'] = this.attachments.map((v) => v.toJson()).toList();
    }
    data['tags'] = this.tags;
    data['referenceInfo'] = this.referenceInfo;
    if (this.feedback != null) {
      data['feedback'] = this.feedback.toJson();
    }
    return data;
  }
}

class User {
  Null requestStatus;
  String id;
  String emailAddress;
  Null description;
  Null links;
  Null credentialsNonExpired;
  Null status;
  String authProvider;
  bool isVerified;
  Null fileId;
  String roles;
  UserDetails userDetails;
  Null fcmToken;
  bool connected;
  bool following;
  BusinessDetails businessDetails;

  User(
      {this.requestStatus,
      this.id,
      this.emailAddress,
      this.description,
      this.links,
      this.credentialsNonExpired,
      this.status,
      this.authProvider,
      this.isVerified,
      this.fileId,
      this.roles,
      this.userDetails,
      this.fcmToken,
      this.connected,
      this.following,
      this.businessDetails});

  User.fromJson(Map<String, dynamic> json) {
    requestStatus = json['requestStatus'];
    id = json['id'];
    emailAddress = json['emailAddress'];
    description = json['description'];
    links = json['links'];
    credentialsNonExpired = json['credentialsNonExpired'];
    status = json['status'];
    authProvider = json['authProvider'];
    isVerified = json['isVerified'];
    fileId = json['fileId'];
    roles = json['roles'];
    userDetails = json['userDetails'] != null
        ? new UserDetails.fromJson(json['userDetails'])
        : null;
    fcmToken = json['fcmToken'];
    connected = json['connected'];
    following = json['following'];
    businessDetails = json['businessDetails'] != null
        ? new BusinessDetails.fromJson(json['businessDetails'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['requestStatus'] = this.requestStatus;
    data['id'] = this.id;
    data['emailAddress'] = this.emailAddress;
    data['description'] = this.description;
    data['links'] = this.links;
    data['credentialsNonExpired'] = this.credentialsNonExpired;
    data['status'] = this.status;
    data['authProvider'] = this.authProvider;
    data['isVerified'] = this.isVerified;
    data['fileId'] = this.fileId;
    data['roles'] = this.roles;
    if (this.userDetails != null) {
      data['userDetails'] = this.userDetails.toJson();
    }
    data['fcmToken'] = this.fcmToken;
    data['connected'] = this.connected;
    data['following'] = this.following;
    if (this.businessDetails != null) {
      data['businessDetails'] = this.businessDetails.toJson();
    }
    return data;
  }
}

class Feedback {
  List<Types> types;
  List<Feedback> feedback;

  Feedback({this.types, this.feedback});

  Feedback.fromJson(Map<String, dynamic> json) {
    if (json['types'] != null) {
      types = new List<Types>();
      json['types'].forEach((v) {
        types.add(new Types.fromJson(v));
      });
    }
    if (json['feedback'] != null) {
      feedback = new List<Feedback>();
      json['feedback'].forEach((v) {
        feedback.add(new Feedback.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.types != null) {
      data['types'] = this.types.map((v) => v.toJson()).toList();
    }
    if (this.feedback != null) {
      data['feedback'] = this.feedback.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Types {
  String name;
  bool isResponded;
  int count;

  Types({this.name, this.isResponded, this.count});

  Types.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    isResponded = json['isResponded'];
    count = json['count'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['isResponded'] = this.isResponded;
    data['count'] = this.count;
    return data;
  }
}

class Feedback {
  String id;
  String userId;
  String displayName;
  String type;
  String date;

  Feedback({this.id, this.userId, this.displayName, this.type, this.date});

  Feedback.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['userId'];
    displayName = json['displayName'];
    type = json['type'];
    date = json['date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['userId'] = this.userId;
    data['displayName'] = this.displayName;
    data['type'] = this.type;
    data['date'] = this.date;
    return data;
  }
}

然后,你可以使用stash_hive包。

import 'dart:io';

import 'package:stash_hive/stash_hive.dart';

class Task {
  final int id;
  final String title;
  final bool completed;

  Task({this.id, this.title, this.completed = false});

  /// Creates a [Task] from json map
  factory Task.fromJson(Map<String, dynamic> json) => Task(
      id: json['id'] as int,
      title: json['title'] as String,
      completed: json['completed'] as bool);

  /// Creates a json map from a [Task]
  Map<String, dynamic> toJson() =>
      <String, dynamic>{'id': id, 'title': title, 'completed': completed};

  @override
  String toString() {
    return 'Task ${id}: "${title}" is ${completed ? "completed" : "not completed"}';
  }
}

void main() async {
  // Temporary path
  final path = Directory.systemTemp.path;

  // Creates cache with a hive based storage backend with the capacity of 10 entries
  final cache = newHiveCache(path,
      maxEntries: 10, fromEncodable: (json) => Task.fromJson(json));
    // Change Task with YourModel!

  // Adds a task with key 'task1' to the cache
  await cache.put(
      'task1', Task(id: 1, title: 'Run stash_hive example', completed: true));
  // Retrieves the value from the cache
  final value = await cache.get('task1');

  print(value);
}

相关问题