我正在学习Flutter,我在构建一个简单的应用程序时遇到了这种奇怪的行为。我已经创建了一个自定义状态窗口小部件,我根本无法在另一个状态窗口小部件中使用它。这是预期的行为还是我在这里遗漏了什么?
错误是:
引发了以下NoSuchMethodError构建 MediaQuery(MediaQueryData(尺寸:大小(375.0, 812.0),devicePixelRatio:3.0,textScaleFactor:1.0,填充:EdgeInsets(0.0,0.0,0.0,34.0), viewInsets:EdgeInsets.zero,alwaysUse24HourFormat:false)): 方法' _debugTypesAreRight'在null上调用。 接收者:null 尝试调用:_debugTypesAreRight(实例' TextCard')
和
抛出了另一个异常:_ScaffoldLayout自定义multichild 布局代表忘了布置下面的孩子:
代码:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Stateful Widget 02',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Testing nested Stateful Widgets'),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new TextCard(),
new TextCard()
],
),
),
);
}
}
class TextCard extends StatefulWidget {
@override
State<StatefulWidget> createState() {
_TextCardState createState() => new _TextCardState();
}
}
class _TextCardState extends State<TextCard> {
@override
Widget build(BuildContext context) {
return new Card(
child: new Text('12312')
);
}
}
答案 0 :(得分:8)
您以错误的方式覆盖createState()
中的TextCard
。
<强>替换强>
createState() {
_TextCardState createState() => new _TextCardState();
}
。通过强>
@override
_TextCardState createState(){
return new _TextCardState();
}