简体   繁体   English

Flutter:在本地存储地图的最佳方式?

[英]Flutter : best way to store maps locally?

what is the best way to store data locally.在本地存储数据的最佳方式是什么。 Assuming that I'm creating an app that stores transaction and I need to save id(because they may be many transactions with the same name), title, amount and time.假设我正在创建一个存储交易的应用程序,我需要保存 id(因为它们可能是许多同名的交易)、标题、金额和时间。 Can I do it with shared preferences or is there another way to do it ?我可以通过共享偏好来做到这一点,还是有其他方法可以做到这一点? Thank you for answering.谢谢你的回答。

您可能会喜欢Hive Hive 是一个功能强大的数据库,如果您正在寻找出色的性能,这就是它

Shared Preference let you read and write key-value pairs in a couple of lines easily. Shared Preference 让您可以轻松地在几行代码中读取和写入键值对。 but shared preference is not a solution for you to keep complex data.但是共享偏好并不是您保留复杂数据的解决方案。 you should keep those data in a Database.您应该将这些数据保存在数据库中。

There are two major types of options you have:您有两种主要类型的选择:

  1. Relational — these are the databases in the traditional sense.关系型——这些是传统意义上的数据库。 They don't just store data but also the relationships between the data.它们不仅存储数据,还存储数据之间的关系。 SQLite is an example of a relational database. SQLite 是关系数据库的一个例子。

SQflite is an implementation of SQLite for Flutter. SQflite是 Fl​​utter 的 SQLite 实现。 It affords you complete control over your database, queries, and relationships.它使您可以完全控制您的数据库、查询和关系。

Pros优点

Total control over the database.完全控制数据库。 A very efficient SQLite database for your app (given that you have pre-existing SQLite knowledge).一个非常高效的 SQLite 数据库,适用于您的应用程序(假设您已经具备 SQLite 知识)。

Cons缺点

Writing out all your queries by hand can take a lot of time.手动写出所有查询可能需要很多时间。

Returned data from the database isn't strongly typed from the start.从数据库返回的数据从一开始就不是强类型的。

It is potentially difficult to handle migrations on the device (when the schema changes between version updates for instance).在设备上处理迁移可能很困难(例如,当模式在版本更新之间发生变化时)。

It has no support for the web.它不支持网络。

  1. NoSQL — these databases store data as documents. NoSQL——这些数据库将数据存储为文档。 A schema is not enforced as is the case with a relational database.不像关系数据库那样强制实施模式。 They are lightning-quick and handle huge unstructured pieces of data very well.它们速度极快,可以很好地处理大量非结构化数据。 MongoDB is an example of a NoSQL database. MongoDB 是 NoSQL 数据库的一个示例。

Hive — offline NoSQL storage Hive——离线 NoSQL 存储

Hive is an extremely fast NoSQL storage option for Flutter developers. Hive 是 Fl​​utter 开发人员非常快速的 NoSQL 存储选项。 Its biggest selling point is that it is completely native to Dart.它最大的卖点是完全原生于 Dart。

Hive lets you store data as a HiveObject, which allows for some relations between objects as well. Hive 允许您将数据存储为 HiveObject,这也允许对象之间的某些关系。 It stores objects in a box, but you can generate TypeAdapters for them.它将对象存储在一个盒子中,但您可以为它们生成 TypeAdapter。

Creating a box is fairly simple:创建一个盒子相当简单:

var box = await Hive.openBox('testBox');

Reading and writing is just as easy:阅读和写作同样简单:

import 'package:hive/hive.dart';
void main() async {
 var box = await Hive.openBox('testBox');
 box.put('name', 'David');
 
 print('Name: ${box.get('name')}');
}

This benchmark if picked up from the hive package readme page:如果从 hive 包自述文件页面获取此基准:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM