坏状态Flutter使用FutureBuilder和Provider中没有任何元素

问题描述

使用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): ══════════════════════════════════════════════

解决方法

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','blue'];
      var newList = list.firstWhere((element) => element.contains(''),orElse: () => 'No matching color found');
      print(newList);
    }

输出:

No matching color found
,

我认为对您来说最简单的方法是将您的房屋更改为有状态的小部件,并更改代码的这一部分。

Navigator.pushNamed(
 context,ViewNoteScreen.routeName,arguments: notes.items[i].id).whenComplete(() {
      setState(() {});
    });