Skip to content Skip to sidebar Skip to footer

Exception Caught By Widgets Library Error In Flutter

I have this application in Flutter. It has two classes it spouses to generate a list of notes. This is the main class, the MyApp class: import 'package:flutter/cupertino.dart'; imp

Solution 1:

You need to wrap your Scaffold with

MaterialApp()

because this is the widget that introduces MediaQuery

Changes in build of MyApp:

returnMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
       ...
      ),
    );

Solution 2:

Wrap your scaffold with MaterialApp

returnMaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    visualDensity: VisualDensity.adaptivePlatformDensity,
  ),
  home: Scaffold(),
);

Solution 3:

Solution

itemCount: locs.length instead of itemCount: 10 .

For those who are curious about the Reason , read the follwing :

Take a look at your List :

 List<Sala> locs = [
  Sala(note: 'Study', noteDes: 'from 6pm ~ 8pm'),
  Sala(note: 'Work', noteDes: 'from 8pm ~ 9pm'),
  Sala(note: 'Play', noteDes: 'from 9pm ~ 9:30pm'),
  Sala(note: 'Eat', noteDes: 'from 9:30pm ~ 10pm'),
];

It's clearly that the Length of your List is 3.

But, here in your ListViewBuilder :

itemCount: 10,

You are setting a static Value of 10, this will tells the builder that there are 10 items to be returned, and that exceed the actual length of your list, to make it clear , You are returning 10 items of Card , while your list only contains 3 item, this will try to create 10 items which is does not exist from the start in your list and of course it will return an Index error when the iterator return the last item of your List,which means it will return the error at the 4th index since you have only 3 items.

Post a Comment for "Exception Caught By Widgets Library Error In Flutter"