我正尝试从数据库中删除数据,如果成功,则继续导航至我的主页。
下面是我的代码:
由lessFromDatabase方法组成的StatelessWidget,该方法传递了一个Id
(字符串)和一个context
:
Consumer<SettingsProvider>(
builder: (context, settingsProvider, child) {
final exerciseSettings = settingsProvider.findById(id);
if (exerciseSettings == null) {
return Center(
child: CircularProgressIndicator(),
);
}
return PreviewExerciseItem(
exerciseSettings: exerciseSettings,
id: id,
navigateToEdit: () =>
_navigateToEditPage(exerciseSettings, context),
deleteFromDatabase: () => _deleteFromDatabase(id, context),
navigateToCountDown: () =>
navigateToCountDownPage(exerciseSettings, context),
);
},
),
从StatelessWidget调用的_deleteFromDatabase方法,并显示一个AlertDialog来确认删除:
void _deleteFromDatabase(String id, context) async {
await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text("Are you sure you want to delete?"),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () async {
try {
Navigator.of(context).pop(true);
await Provider.of<SettingsProvider>(context, listen: false)
.deleteFromList(id);
Navigator
.pushNamedAndRemoveUntil(context,HomePage.routeName, (Route route) => route.isFirst);
} catch (e) {
print(e);
}
},
child: new Text('Yes'),
),
],
),
);
}
deleteFromList方法来自“我的提供者”类:
Future<void> deleteFromList(String id) async{
try{
final _itemIndex = _items.indexWhere((item) => item.id == id);
await _databaseHelper.deleteExercises(id);
_items.removeAt(_itemIndex);
notifyListeners();
}catch(e){
print(e);
}
}
提供商类的findById:
CustomExercise findById(String id) {
return _items.firstWhere((prod) => prod.id == id);
}
注意:我能够从数据库中成功删除数据,但是就在导航到我的主页之前,一瞬间出现了一个错误,显示为红色屏幕:错误状态:无元素
下面是我的日志中的完整错误消息:
在构建Consumer(脏,依赖项:[_ InheritedProviderScope,_InheritedTheme,_LocalizationsScope- [GlobalKey#5ce12]])时引发了以下StateError: 错误状态:无元素
相关的引起错误的小部件是:
Consumer<SettingsProvider>
引发异常时,这是堆栈:
#0 ListMixin.firstWhere(dart:collection / list.dart:150:5)
#1 SettingsProvider.findById(软件包:workoutapp / providers / settings_provider.dart:12:19)
#2 PreviewExercisePage.build。<anonymous closure>
(程序包:workoutapp / pages / preview_exercise_page.dart:68:55)
#3 Consumer.buildWithChild(package:provider / src / consumer.dart:175:19)
#4 SingleChildStatelessWidget.build(package:nested / nested.dart:260:41)
答案 0 :(得分:1)
当列表为空或者第一个元素为空时会发生这种情况,因此您应该检查列表是否为空。
List list = [];
print(list[0])
确定您会收到这样的消息:
Unhandled exception:
Bad state: No element
#0 List.first (dart:core-patch/growable_array.dart:332:5)
#1 main (file:///C:/Users/onbody/AndroidStudioProjects/nmae/bin/name.dart:9:14)
#2 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
解决办法是:
List list = [];
if (list.isNotEmpty) {
print(list[0]);
} else {
print('the list is empty'!);
}
我希望这对某人有帮助谢谢!
答案 1 :(得分:0)
如注释中先前所述,很可能检查空列表的值会导致错误。一种解决方法是检查CustomExercise findById(String)
和deleteFromList(String)
上的列表是否为空。
即
if(_items != null && _items.length > 0)