Flutter 中的 Widgets 库错误捕获异常

Far*_*Txr 5 dart android-studio flutter flutter-dependencies flutter-layout

我在 Flutter 中有这个应用程序。它有两个类来生成笔记列表。

\n

这是主类,MyApp 类:

\n
import 'package:flutter/cupertino.dart';\nimport 'package:flutter/material.dart';\nimport 'package:notesgenerator/sala.dart';\n\nvoid main() => runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n  // This widget is the root of your application.\n  @override\n\n  Widget build(BuildContext context) {\n\n    List<Sala> locs = [\n      Sala(note: 'Study', noteDes: 'from 6pm ~ 8pm'),\n      Sala(note: 'Work', noteDes: 'from 8pm ~ 9pm'),\n      Sala(note: 'Play', noteDes: 'from 9pm ~ 9:30pm'),\n      Sala(note: 'Eat', noteDes: 'from 9:30pm ~ 10pm'),\n    ];\n\n    return Scaffold(\n      appBar: AppBar(\n        title: Text('NoteIndex'),\n        centerTitle: true,\n      ),\n      body: ListView.builder(\n          itemCount: 10,\n          itemBuilder: (context, index) {\n            return Card(\n              child: ListTile(\n                onTap: () {},\n                title: (Text(locs[index].note)),\n              ),\n            );\n          }),\n    );}}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
class Sala {\n  String note;\n  String noteDes;\n  Sala({this.note, this.noteDes});\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

如果您能提供帮助,我将非常感激!如果您有任何疑问,请在评论中告诉我!

\n

Cha*_*leb 6

用 MaterialApp 包裹你的脚手架

    return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    visualDensity: VisualDensity.adaptivePlatformDensity,
  ),
  home: Scaffold(),
);
Run Code Online (Sandbox Code Playgroud)


小智 5

你需要用以下方式包裹你的脚手架

材质应用程序()

因为这是引入MediaQuery的小部件

MyApp 构建的变化:

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
       ...
      ),
    );
Run Code Online (Sandbox Code Playgroud)


小智 5

解决方案

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'),
];
Run Code Online (Sandbox Code Playgroud)

很明显,列表的长度是 3。

但是,在您的ListViewBuilder中:

itemCount: 10,
Run Code Online (Sandbox Code Playgroud)

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