2

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): ══════════════════════════════════════════════
c49
  • 360
  • 1
  • 7
  • 18
  • shouldn't `_items.remove(position);` be `_items.removeAt(position);` ? – Kise Oct 02 '20 at 09:58
  • not working.. I already checked. actually, it deletes the data but shows red screen with bad state no element. – c49 Oct 02 '20 at 10:00
  • Please show me the type of the `_items` variable. – Kise Oct 02 '20 at 10:03
  • You probably access an item which does not exist in the list. For example, accessing `_items[3]` while `_items` only has length < 2, or using `first`, `firstWhere`, `last`, `lastWhere` without supplying an `orElse` parameter. Now what you should do is using the `Find Usages` on the `_items` variable to see if you accidentally use it like I suggest. – Kise Oct 02 '20 at 10:13
  • actually, I think am not. I will show you more code..one minute – c49 Oct 02 '20 at 10:17
  • It shows when home.dart opens – c49 Oct 02 '20 at 10:18
  • I have updated the question with code – c49 Oct 02 '20 at 10:21
  • 1
    I see a bunch of `_items.firstWhere` statements in your code. Are you sure those do not cause the problem? If possible please post the stacktrace also. – Kise Oct 02 '20 at 10:23
  • I am not sure... what is happening.. okay.. i will post stacktrace also – c49 Oct 02 '20 at 10:24
  • stacktrace added – c49 Oct 02 '20 at 10:27
  • 1
    the line that causes the problem is `final selectedNote = provider.findById(id);` inside `_ViewNoteScreenState`. It is clearly stated in the stacktrace, you are using `firstWhere` to find the note with the specified `id`, but it doesn't exist. What you should do is using `indexWhere` to find the index of the note, and if it's >= 0, then the note exists. – Kise Oct 02 '20 at 10:31

2 Answers2

9

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
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • @c49: see edited answer, if item is not available in list, then you need to handle it by adding orElse – Jitesh Mohite Oct 03 '20 at 06:13
  • yeah..found it.. but the problem is it is calling after deleting due to Consumer or Provider. How to avoid calling that? – c49 Oct 03 '20 at 06:59
  • Either add listen: false, but I think should be call, and to handle this you must return null, from method and handle this null inside widget, Ex: Don't show widget if its nulll – Jitesh Mohite Oct 03 '20 at 07:26
  • that's the problem, can't return null in findbyid – c49 Oct 03 '20 at 08:04
  • why? you can't return null – Jitesh Mohite Oct 03 '20 at 08:09
  • the container is due to can't return null in build methods. – c49 Oct 03 '20 at 08:35
  • This condition should in class from where you are calling ViewNoteScreen Widget, There you can add a null condition whether to show it or not. I think this will work for you, could you please accept the answer. – Jitesh Mohite Oct 03 '20 at 08:47
  • That won't work. viewscreen is the child of the consumer. That's why viewscreen gets called automatically, when an item deleted. that's how it calls findbyid. – c49 Oct 03 '20 at 09:08
  • Either you have to find a way to stop calling ViewNoteScreen Widget, or show empty container inside it, both are fine. – Jitesh Mohite Oct 03 '20 at 09:11
  • I have tried all ways that I know, to stop calling ViewNoteScreen automatically. is showing empty container makes memory leak? because I think ViewNoteScreen is showing container behind the home screen. – c49 Oct 03 '20 at 09:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222439/discussion-between-jitsm555-and-c49). – Jitesh Mohite Oct 03 '20 at 09:20
  • same problem..however he solved it.. he says in comment...https://stackoverflow.com/questions/62605192/flutter-bad-state-no-element – c49 Oct 03 '20 at 09:36
0

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(() {});
    });
Sajjad
  • 2,593
  • 16
  • 26