Bad state no element in Flutter Using FutureBuilder and Provider

13k views Asked by At

Using Provider and FutureBuilder, I just deleted data from database, it got removed at the same time, it shows a red screen with bad state no element. There is no problem to add data, do not show this problem. Code is given below

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): ══════════════════════════════════════════════
2

There are 2 answers

13
Jitesh Mohite On BEST ANSWER

BadStateException occurred when list don't contain the item and still someone searching for that

Example:

If orElse not defined in the code and the wrong item gets search which doesn't exist in the list then it shows BadStateException

 void main() {
      List<String> list = ['red', 'yellow', 'pink', 'blue'];
      var newList = list.firstWhere((element) => element.contains('green'));
      print(newList);
    }

Output:

Uncaught Error: Bad state: No element

Solution: Add orElse

 void main() {
      List<String> list = ['red', 'yellow', 'pink', 'blue'];
      var newList = list.firstWhere((element) => element.contains(''), 
          orElse: () => 'No matching color found');
      print(newList);
    }

Output:

No matching color found
0
Sajjad On

I think the easiest way for you is change your home to statefull widget and change this part of your code.

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