我正在学习Flutter,我在构建一个简单的应用程序时遇到了这种奇怪的行为。我已经创建了一个自定义的有状态小部件,但我根本无法在另一个有状态小部件中使用它。是预期的行为还是我在这里遗漏了什么?
错误是:
以下 NoSuchMethodError 被抛出构建 MediaQuery(MediaQueryData(size: Size(375.0, 812.0(, 设备像素比率: 3.0, 文本比例因子: 1.0, 填充: 边缘插入(0.0, 0.0, 0.0, 34.0(, viewInset: EdgeInsets.zero, alwaysUse24HourFormat: false((: 方法 '_debugTypesAreRight' 在 null 上调用。 接收器:空 尝试调用:_debugTypesAreRight("文本卡"的实例(
和
抛出了另一个异常:_ScaffoldLayout自定义多子级 布局委托忘记布局以下子项:
代码:
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')
);
}
}
错误的方式TextCard
覆盖createState()
。
取代:
createState() {
_TextCardState createState() => new _TextCardState();
}
跟:
@override
_TextCardState createState(){
return new _TextCardState();
}