dart Flutter中的Exception Caught By Widgets Library错误

w9apscun  于 2023-06-27  发布在  Flutter
关注(0)|答案(4)|浏览(238)

我在Flutter中有这个应用程序。它有两个类,它们相互匹配以生成注解列表。
这是主类,MyApp类:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:notesgenerator/sala.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override

  Widget build(BuildContext context) {

    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'),
    ];

    return Scaffold(
      appBar: AppBar(
        title: Text('NoteIndex'),
        centerTitle: true,
      ),
      body: ListView.builder(
          itemCount: 10,
          itemBuilder: (context, index) {
            return Card(
              child: ListTile(
                onTap: () {},
                title: (Text(locs[index].note)),
              ),
            );
          }),
    );}}

这是存储数据的类,Sala类:

class Sala {
  String note;
  String noteDes;
  Sala({this.note, this.noteDes});
}

当我试着运行它时,我得到了这个:

I/flutter (21388): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (21388): The following assertion was thrown building MyApp:
I/flutter (21388): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (21388): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (21388): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (21388): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
I/flutter (21388): The context used was:
I/flutter (21388):   Scaffold
I/flutter (21388): 
I/flutter (21388): The relevant error-causing widget was:
I/flutter (21388):   MyApp file:///F:/FlutterProjects/notesgenerator/lib/main.dart:6:23
I/flutter (21388): 
I/flutter (21388): When the exception was thrown, this was the stack:
I/flutter (21388): #0      MediaQuery.of (package:flutter/src/widgets/media_query.dart:798:5)
I/flutter (21388): #1      ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:1993:50)
I/flutter (21388): #2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4376:12)
I/flutter (21388): #3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter (21388): #4      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (21388): #5      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (21388): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter (21388): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter (21388): #8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
I/flutter (21388): #9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter (21388): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (21388): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (21388): #12     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1028:16)
I/flutter (21388): #13     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:999:5)
I/flutter (21388): #14     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:942:17)
I/flutter (21388): #15     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2412:19)
I/flutter (21388): #16     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:941:13)
I/flutter (21388): #17     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:819:7)
I/flutter (21388): #18     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:804:7)
I/flutter (21388): #27     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
I/flutter (21388): #28     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
I/flutter (21388): #29     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
I/flutter (21388): (elided 8 frames from package dart:async and package dart:async-patch)
I/flutter (21388): 
I/flutter (21388): ════════════════════════════════════════════════════════════════════════════════════════════════════

如果你能帮忙,我会非常感激的!如果你有任何问题,请在评论中告诉我!

eeq64g8w

eeq64g8w1#

使用MaterialApp包裹您的脚手架

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

hgtggwj02#

你需要把脚手架包起来
MaterialApp()
因为这是引入MediaQuery的小部件
MyApp版本的更改:

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

qlvxas9a3#

解决方案

itemCount: locs.length而不是itemCount: 10

对于那些好奇的原因,请阅读以下内容:

看看你的清单:

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'),
];

显然你的列表长度是3。

但是,在您的ListViewBuilder中:

itemCount: 10,

你设置了一个静态值10,这将告诉构建器有10个项目要返回,并且超过了你的列表的实际长度,为了清楚起见,你返回了10个Card项目,而你的列表只包含3个项目,这将尝试创建10个项目,这是不存在的,从一开始就在您的列表中,当然,它会返回一个索引错误时,迭代器返回的最后一个项目的列表,这意味着它将在第四个索引处返回错误,因为您只有3个项目。

2ul0zpep

2ul0zpep4#

如果你使用的是Getxbuilder,然后按照说明:
首先,你必须验证你<YOUR_VIEW_CONTROLER>的Getxbuilder名称的每一个Getxbuilder视图控制器。
这是我得到错误时的唯一问题。
代码如下:

GetBuilder<**RecentMatchListViewController**>(
                            builder: (_) => recentMatchListViewController!
                                        .recentMatchListModel!
                                        .data![index]
                                        .result ==
                                    null
                                ? progressIndicator(1)
                                : Text(
                                    recentMatchListViewController!
                                        .recentMatchListModel!
                                        .data![index]
                                        .result
                                        .toString(),
                                    style: redHeadingTextStyle(),
                                    textAlign: TextAlign.center,
                                  ),
                          )

RecentMatchListViewController可能会更改为另一个视图控制器,如

GetBuilder<**UpcomingMatchListViewController**>(
                            builder: (_) => recentMatchListViewController!
                                        .recentMatchListModel!
                                        .data![index]
                                        .result ==
                                    null
                                ? progressIndicator(1)
                                : Text(
                                    recentMatchListViewController!
                                        .recentMatchListModel!
                                        .data![index]
                                        .result
                                        .toString(),
                                    style: redHeadingTextStyle(),
                                    textAlign: TextAlign.center,
                                  ),
                          )

这是由于一些复制过去的工作,也导入了不匹配的文件.

相关问题