我正在构建一个 Flutter 应用程序,我希望将数据保持在离线状态。
我正在使用相机或图库图像选择器捕获图像,并且能够将该图像存储到File image变量中。
File _avatarImg;
void _getImage(BuildContext context, ImageSource source) {
ImagePicker.pickImage(
source: source,
maxWidth: 400.0,
maxHeight: 400.0,
).then((File image) {
_avatarImg = image;
});
}
Run Code Online (Sandbox Code Playgroud)
这非常有效,但我的问题是,我将如何存储此图像以保持持久性?我是否应该在图像所在的手机中存储指向本地媒体目录的字符串链接?如果是这样,我会担心图像是否被用户意外删除。或者我是否将图像本身存储到 BLOB 中的数据库中?这方面的最佳做法是什么?
目前我正在通过 initState 分配所有变量,但是我看到没有必要通过 initState 分配变量,因为我可以直接为变量分配一个值。这些作业的顺序是什么?它们有何不同?为什么以及什么时候你会选择一个而不是另一个?
class Person {
String name = "John";
@override
void initState(){
....
....
}
}
Run Code Online (Sandbox Code Playgroud)
对比
class Person {
String name;
@override
void initState(){
name = "John";
}
}
Run Code Online (Sandbox Code Playgroud) 在我的 NativeScript 应用程序中,我将路由设置为
export const routes = [
{ path: "", component: AppComponent},
{ path: "home", component: HomeComponent},
{ path: "login", component: LoginComponent},
{ path: "news" , component: NewsComponent}
];
Run Code Online (Sandbox Code Playgroud)
用户通过 firebase 登录后,用户会自动重定向到HomeComponent. 我应该提一下,我有一个用户服务,它从AppComponent Constuctor确定用户是否已经登录然后将他们重定向到登录或主页中被触发。
我将 console.log() 分布在整个组件中,以查看触发事件的时间,以便我可以尝试找出代码的放置位置。
import {Component, OnInit} from "@angular/core";
import {Page} from "ui/page";
import {RouterExtensions} from 'nativescript-angular/router';
import firebase = require("nativescript-plugin-firebase");
@Component ({
selector : "home",
templateUrl: "./pages/home/home.html",
styleUrls: ["./pages/home/home.css"]
})
export class HomeComponent implements OnInit {
private router;
constructor(page: Page, router: …Run Code Online (Sandbox Code Playgroud) 我将列表保存到 Hive Box 中的索引中。
class Person {
String name;
Person(this.name);
}
List<Person> friends = [];
friends.add(Person('Jerry'));
var accountBox = Hive.openBox('account');
accountBox.put('friends',friends);
//Testing as soon as saved to make sure it's storing correctly.
List<Person> friends = accountBox.get('friends');
assert(friends.length == 1);
Run Code Online (Sandbox Code Playgroud)
所以这一切都按预期进行。由于某些疯狂的原因,当我热重启应用程序并尝试从 Hive 获取好友列表时,它不再返回List<Person>. 它返回一个List<dynamic>
var accountBox = Hive.openBox('account');
List<Person> friends = accountBox.get('friends');
///ERROR
E/flutter (31497): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled
Exception: type 'List<dynamic>' is not a subtype of type 'List<Person>'
E/flutter (31497): <asynchronous suspension>
etc...
Run Code Online (Sandbox Code Playgroud)
可能是什么原因造成的?这太不寻常了。
我正在尝试从 sqlite 中的 Text Column 进行基本抓取,并将其处理为List<String>. 如果数据为空,那么我想将其设置为空列表[]。但是,当我出于某种原因执行此操作时,我得到一个看起来为空但实际上长度为 1 的列表。为了重现此问题,我使用以下内容简化了该问题。
String stringList = '';
List<String> aList = [];
aList = stringList.split(',');
print(aList.length);
Run Code Online (Sandbox Code Playgroud)
为什么会打印 1?它不应该返回 0,因为它没有带逗号的值吗?
我将在作用域模型和共享首选项中存储许多小数据字符串。我的问题是,为了检索这些数据,从这些来源中的任何一个检索这些数据是否存在显着的速度差异?由于我将进行许多“设置”和“获取”,我想知道是否有人看到使用一种比另一种更多的性能差异。
我知道共享首选项是持久的,而范围模型不是,但是在加载应用程序后,数据会同步,我宁愿从最快的来源访问数据。
dart ×5
flutter ×4
database ×2
class ×1
flutter-hive ×1
list ×1
nativescript ×1
oop ×1
performance ×1
sqlite ×1