在 null 上调用了 getter 'classification'
Posted
技术标签:
【中文标题】在 null 上调用了 getter \'classification\'【英文标题】:getter 'classification' was called on null在 null 上调用了 getter 'classification' 【发布时间】:2020-01-10 18:40:45 【问题描述】:我大约两周大的时候开始颤抖,我不明白这个错误的真正含义。我将在下面添加堆栈:
List<DropdownMenuItem> showDropdownMenus()
return CLASSIFICATION.map((item)
return DropdownMenuItem(
value: item,
child: Text(item),
);
).toList();
错误堆栈:
I/flutter (4403):在构建 RegistrationPage(dirty, state: 我/颤振(4403):RegistrationPageState#21643): I/flutter (4403):在 null 上调用了 getter 'classification'。 I/颤振(4403):接收器:空 I/flutter(4403):尝试调用:分类 我/颤动(4403): I/flutter (4403):当异常被抛出时,这是堆栈: I/flutter (4403): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5) I/flutter (4403): #1 RegistrationPageState.build (package:lesson2/view/registrationpage.dart:99:44) I/flutter (4403): #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4012:27) I/flutter (4403): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15) I/flutter (4403): #4 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #5 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #10 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #13 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #14 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #15 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #16 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #28 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403):#31 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #32 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #33 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #34 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #35 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403):#38 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #39 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #40 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #41 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #43 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #45 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #46 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #47 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #48 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #49 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #50 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #51 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #52 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #53 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #54 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #55 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #56 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #57 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #58 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #59 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #60 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #61 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #62 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #63 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #64 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #65 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #66 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #67 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #68 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #69 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #71 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #72 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #73 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #74 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #75 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #76 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14) I/flutter (4403): #77 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #78 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #79 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #80 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #81 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #82 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #83 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #84 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #85 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #86 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #87 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #88 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #89 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #90 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #91 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #92 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #93 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #94 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5) I/flutter (4403): #95 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11) I/flutter (4403): #96 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5) I/flutter (4403): #97 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14) I/flutter (4403): #98 Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12) I/flutter (4403): #99 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4875:32) I/flutter (4403): #100 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5208:17) I/flutter (4403): #101 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #102 _TheatreElement.update (package:flutter/src/widgets/overlay.dart:607:16) I/flutter (4403): #103 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #104 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #105 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #106 StatefulElement.update (package:flutter/src/widgets/framework.dart:4085:5) I/flutter (4403): #107 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #108 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #109 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #110 ProxyElement.update (package:flutter/src/widgets/framework.dart:4219:5) I/flutter(4403):#111 _InheritedNotifierElement.update(包:flutter/src/widgets/inherited_notifier.dart:94:11) I/flutter (4403): #112 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #113 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5099:14) I/flutter (4403): #114 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #115 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #116 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #117 StatefulElement.update (package:flutter/src/widgets/framework.dart:4085:5) I/flutter (4403): #118 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #119 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5099:14) I/flutter (4403): #120 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #121 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5099:14) I/flutter (4403): #122 Element.updateChild (package:flutter/src/widgets/framework.dart:2876:15) I/flutter (4403): #123 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16) I/flutter (4403): #124 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5) I/flutter (4403): #125 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2340:33) I/flutter (4403): #126 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20) I/flutter (4403): #127 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5) I/flutter (4403): #128 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15) I/flutter (4403): #129 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9) I/flutter (4403): #130 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:874:5) I/flutter (4403):#134 _invoke (dart:ui/hooks.dart:236:10) I/flutter (4403): #135 _drawFrame (dart:ui/hooks.dart:194:3) I/flutter(4403):(从包 dart:async 中省略了 3 帧)
【问题讨论】:
【参考方案1】:您的 CLASSIFICATION 可能未正确初始化。
请查看下面的示例
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
List<Map> _jsonApi1 = [
"id": 0, "name": "default 1"
];
List<Map> _jsonApi2 = [
"id": 1, "name": "second 2",
"id": 2, "name": "third 3"
];
List<Map> _myJson = new List.from(_jsonApi1)..addAll(_jsonApi2);
class _MyHomePageState extends State<MyHomePage>
String _mySelection;
@override
Widget build(BuildContext context)
return new Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Container(
height: 500.0,
child: new Center(
child: new DropdownButton<String>(
isDense: true,
hint: new Text("$_jsonApi1[0]["name"]"),
value: _mySelection,
onChanged: (String newValue)
setState(()
_mySelection = newValue;
);
print(_mySelection);
,
items: _myJson.map((Map map)
return new DropdownMenuItem<String>(
value: map["id"].toString(),
child: new Text(
map["name"],
),
);
).toList(),
),
),
),
],
),
),
);
【讨论】:
你是对的。我没有正确初始化它,但 VS 代码也没有抛出错误,所以我不知道我的代码有什么问题。我想到了。谢谢!以上是关于在 null 上调用了 getter 'classification'的主要内容,如果未能解决你的问题,请参考以下文章
例外:NoSuchMethodError:在 null 上调用了 getter 'uid'。接收方:null 尝试调用:uid
错误“在 null 上调用了 getter 'userEmail'
在 null 上调用了 getter '_controller'。扑