0

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.

  • `_DatapointsRowState(this.datapoints, this.Index);` should be ` _DatapointsRowState(this.datapoints, this.datapointsIndex);` but I guess this is only a mistake in the question. But I'm pretty sure if an object works but an int doesn't it's just because an error of this kind. – Günter Zöchbauer Feb 06 '18 at 07:33
  • Gunter you are correct - I actually made my simplified code so that you can easily copy-paste over and run. I agree with your thoughts, but I just cannot find the error. – Mark Gouveia Feb 06 '18 at 13:09
  • Is `datapointsIndex` not initialized at the beginning and then later updated but the UI not rebuilt (update not within `setState(() {...})`? – Günter Zöchbauer Feb 06 '18 at 13:11
  • I just found the error on my end. Thank you for looking. I have updated my code above so that it works. – Mark Gouveia Feb 06 '18 at 13:25
  • Great :D . . . . . – Günter Zöchbauer Feb 06 '18 at 13:31
  • FYI, you can answer your own question on stackoverflow, so that this question becomes "answered" – TruongSinh Mar 02 '19 at 10:34

0 Answers0