flutter - 在颤振中将数据添加到本地存储
问题描述
我使用https://pub.dev/packages/localstorage在我的 Flutter 应用程序中创建本地存储。
我在这个“事件”小部件中创建了一些与这个本地存储一起使用的方法
class Event extends StatefulWidget {
final eventInfo;
Event({Key key, List eventInfo})
: this.eventInfo = eventInfo,
super(key: key);
@override
_EventState createState() => _EventState();
}
class FavName {
String eventName;
FavName({this.eventName});
get favEventName {
return eventName;
}
toJSONEncodable() {
Map<String, dynamic> m = new Map();
m['Name'] = eventName;
return m;
}
}
class FavList {
List<FavName> favNameList;
FavList() {
favNameList = new List();
}
toJSONEncodable() {
return favNameList.map((item) {
return item.toJSONEncodable();
}).toList();
}
}
class _EventState extends State<Event> {
final FavList list = new FavList();
final LocalStorage storage = new LocalStorage('favList');
addItem(String eventName) {
setState(() {
final item = new FavName(eventName: eventName);
list.favNameList.add(item);
_saveToStorage();
});
}
deleteItem(String eventName) {
setState(() {
final item = new FavName(eventName: eventName);
list.favNameList.remove(item);
_saveToStorage();
});
}
_saveToStorage() {
storage.setItem('favList', list.toJSONEncodable());
}
. . .
所以我想用一个按钮将一个字符串添加到我的本地存储中。
这是我的按钮代码。
child: IconButton(
icon: Icon(
Icons.star,
color: _iconColors,
),
onPressed: () {
print(name);
(storage.getItem(name)) ? deleteItem(name)
: addItem(name);
setState(() {
if (storage.getItem(name) == null) {
_iconColors = Colors.grey;
} else {
_iconColors = Colors.yellow;
}
});
})
但是当我点击按钮时,我得到了这个错误
I/flutter (27489): 2-Art Event //Print the string that I want to add to the local storage
════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Failed assertion: boolean expression must not be null
When the exception was thrown, this was the stack
#0 _EventState.buildInfo.<anonymous closure>
package:lx_building/screens/event.dart:190
#1 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:772
#2 _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:855
#3 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:182
#4 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:522
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#1f697
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(324.7, 264.3)
finalLocalPosition: Offset(32.4, 9.7)
button: 1
sent tap down
════════════════════════════════════════════════════════════════════════════════
我怎样才能解决这个问题?
解决方案
storage.getItem(name)
返回存储的数据,通常是Map
.
在您的情况下,您必须使用布尔值。您可以测试您的回报不为 null :
(storage.getItem(name) != null) ? deleteItem(name)
: addItem(name);
推荐阅读
- unreal-engine4 - 物理模拟的 Pawn 抓钩
- android - Xamarin Android CookieManager 不存储所有 cookie
- javascript - 在数组数组中查找最大/最小元素的最有效方法
- c# - RavenDB 的 BulkInsert 错误:文档 ID 必须具有非空值
- symfony - Symfony 4 ROLE_USER 到 ROLE_ADMIN 与数据库
- java - 使用 iText 阅读 PDF 中以图像形式给出的复选框
- angular - 比较反应形式以仅过滤掉更新的值
- javascript - AWSIoT:AWSIoTShadowClient 与 AWSIoTMQTTClient
- android - Android Nougat 通知操作按钮未显示在通知中
- cors - 通过跨域重定向透明移动启用 CORS 的资源