Im learning Flutter and I came across this strange behaviour while building a simple app. I've created a custom Stateful Widget and I simply can't use it inside another Stateful Widget. Is it the expected behaviour or I'm missing something here?
The errors are:
The following NoSuchMethodError was thrown building MediaQuery(MediaQueryData(size: Size(375.0, 812.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, padding: EdgeInsets(0.0, 0.0, 0.0, 34.0), viewInsets: EdgeInsets.zero, alwaysUse24HourFormat: false)): The method '_debugTypesAreRight' was called on null. Receiver: null Tried calling: _debugTypesAreRight(Instance of 'TextCard')
and
Another exception was thrown: The _ScaffoldLayout custom multichild layout delegate forgot to lay out the following child:
The code:
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')
);
}
}
You are overriding createState()
in TextCard
the wrong way.
Replace:
createState() {
_TextCardState createState() => new _TextCardState();
}
With:
@override
_TextCardState createState(){
return new _TextCardState();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With