使用Provider和FutureBuilder,我刚刚从数据库中删除了数据,同时删除了它,它显示红色屏幕,状态为无元素。添加数据没有问题,不显示此问题。 代码在下面给出
stacktrace
ter (12519): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (12519): The following StateError was thrown building ViewNoteScreen(dirty, dependencies:
I/flutter (12519): [_ModalScopeStatus], state: _ViewNoteScreenState#f589b):
I/flutter (12519): Bad state: No element
I/flutter (12519):
I/flutter (12519): The relevant error-causing widget was:
I/flutter (12519): ViewNoteScreen file:///C:/Users/Admin/Desktop/daily%2010/provider_note/lib/main.dart:21:46
I/flutter (12519):
I/flutter (12519): When the exception was thrown, this was the stack:
I/flutter (12519): #0 ListMixin.firstWhere (dart:collection/list.dart:148:5)
I/flutter (12519): #1 Providers.findById (package:provider_note/helper/provider.dart:14:19)
I/flutter (12519): #2 _ViewNoteScreenState.build (package:provider_note/screens/view_note.dart:21:35)
I/flutter (12519): #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
I/flutter (12519): #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
I/flutter (12519): #5 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
I/flutter (12519): #6 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
I/flutter (12519): #7 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:33)
I/flutter (12519): #8 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:866:20)
I/flutter (12519): #9 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5)
I/flutter (12519): #10 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
I/flutter (12519): #11 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1056:9)
I/flutter (12519): #12 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:972:5)
I/flutter (12519): #16 _invoke (dart:ui/hooks.dart:253:10)
I/flutter (12519): #17 _drawFrame (dart:ui/hooks.dart:211:3)
I/flutter (12519): (elided 3 frames from dart:async)
I/flutter (12519):
I/flutter (12519): ══════════════════════════════════════════════
答案 0 :(得分:2)
BadStateException
是在列表中不包含该项目并且仍然有人在搜索该项目
示例:
如果未在代码中定义orElse
,并且错误的项目得到了列表中不存在的搜索,则显示BadStateException
void main() {
List<String> list = ['red', 'yellow', 'pink', 'blue'];
var newList = list.firstWhere((element) => element.contains('green'));
print(newList);
}
输出:
Uncaught Error: Bad state: No element
解决方案:添加或其他
void main() {
List<String> list = ['red', 'yellow', 'pink', 'blue'];
var newList = list.firstWhere((element) => element.contains(''),
orElse: () => 'No matching color found');
print(newList);
}
输出:
No matching color found
答案 1 :(得分:0)
我认为对您来说最简单的方法是将您的房屋更改为有状态的小部件,并更改代码的这一部分。
Navigator.pushNamed(
context, ViewNoteScreen.routeName,
arguments: notes.items[i].id).whenComplete(() {
setState(() {});
});