I am having an issue with a integer I am passing that becomes null inside a class. When I pass a full class everything works fine, but when I pass just an integer it fails. I could put the number inside my class, but I would prefer not to.
I am assuming it is a result of my StatefulWidget inside of a StatefulWidget. But since the class works fine, I can't figure out why a simple integer won't work.
I have simplified my code below for the part that does not work and the resulting snapshot of the resulting screen.
! https://i.stack.imgur.com/ajAkT.jpg
EDIT - Stupid error on my end. Keeping the edited working code snippet below for other's reference on a StatefulWidget inside of a StatefulWidget
import 'package:flutter/material.dart';
void main() {
runApp(
new MaterialApp(
title: "Test",
home: new DataPointPage(),
),
);
}
class DatapointsPoint {
final String type;
final int point1;
final int point2;
const DatapointsPoint({this.type, this.point1, this.point2});
}
_DataPointPageState _dataPointPageState = new _DataPointPageState();
class DataPointPage extends StatefulWidget {
DataPointPage({Key key, this.title}) : super(key: key);
final String title;
@override
_DataPointPageState createState() => _dataPointPageState;
}
class _DataPointPageState extends State<DataPointPage> {
final TextEditingController _controller = new TextEditingController();
var datapoints = <DatapointsPoint>[
const DatapointsPoint(type: 'A', point1: 10, point2: 200),
const DatapointsPoint(type: 'B', point1: 30, point2: 400),
const DatapointsPoint(type: 'C', point1: 50, point2: 500),
];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('myApp'),
),
body: new ListView.builder(
itemCount: datapoints == null ? 0 : datapoints.length,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
new DatapointsRow(datapoints[index], index),
],
);
}),
);
}
}
class DatapointsRow extends StatefulWidget {
DatapointsRow(this.datapoints, this.datapointsIndex);
final DatapointsPoint datapoints;
final int datapointsIndex;
@override
_DatapointsRowState createState() =>
new _DatapointsRowState(datapoints, datapointsIndex);
}
class _DatapointsRowState extends State<DatapointsRow> {
final DatapointsPoint datapoints;
final int datapointsIndex;
_DatapointsRowState(this.datapoints, this.datapointsIndex);
@override
Widget build(BuildContext context) {
return new Card(
child: new Column(
children: <Widget>[
new Padding(padding: new EdgeInsets.all(11.0)),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new CircleAvatar(
child: new Text(
datapointsIndex.toString()) //<-- This returns null
),
new Text(datapoints.type) //<-- This works fine
],
),
],
),
);
}
}
FYI - I am using the global instance of _dataPointPageState since I have a StatefulWidget inside a StatefulWidget per this post: How to Set/Update Sate of StatefulWidget from other StatefulWidget in Flutter? . If I should use a different method let me know.