简体   繁体   中英

The method '>' was called on null. Receiver: null Tried calling: >(1e-10) The relevant error-causing widget was Column

I was writing this piece of code:

Column(
          children: <Widget>[
            GridView.count(
              crossAxisSpacing: 10,
              mainAxisSpacing: 1,
              crossAxisCount: 7,
              children: <Widget>[
                for (int c = 0; c < 10 /*0000*/; c++)
                  InkWell(
                    child: Container(
                      padding: EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: dateColors[c],
                      ),
                      child: setDay(c),
                    ),
                    onTap: () {
                      setState(() {
                        changeColors(c);
                      });
                    },
                  ),
              ],
            ),
            ListView(
              shrinkWrap: true,
              children: <Widget>[
                Text("I'm dedicating every day to you"),
                Text('Domestic life was never quite my style'),
                Text('When you smile, you knock me out, I fall apart'),
                Text('And I thought I was so smart'),
              ],
            ),
          ],
        ),

I wanted to make a scrollable grid with a scrollable list under that, but I want to make sure that if I scroll the grid the list isn't scrolled and vice versa. The code above doesn't work. Does anyone know why? I'm still a newbie in Flutter and I'm trying to learn it.

just use method to create list of inkWell s and call it as children of the GridView.

Column(
      children: <Widget>[
        GridView.count(
          crossAxisSpacing: 10,
          mainAxisSpacing: 1,
          crossAxisCount: 7,
          children: _createInkWellWidgets(),
        ),
        ListView(
          shrinkWrap: true,
          children: <Widget>[
            Text("I'm dedicating every day to you"),
            Text('Domestic life was never quite my style'),
            Text('When you smile, you knock me out, I fall apart'),
            Text('And I thought I was so smart'),
          ],
        ),
      ],
    )

List<Widget> _createInkWellWidgets() {
  List<Widget> widgets = [];
  for (int c = 0; c < 10; c++) {
    widgets.add(InkWell(
      child: Container(
        padding: EdgeInsets.all(8),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: dateColors[c],
        ),
        child: setDay(c),
      ),
      onTap: () {
        setState(() {
          changeColors(c);
        });
      },
    ));
  }
  return widgets;
}

You missed square brackets after for statement.

If you need to recap how to use for in a widget tree take a look here How to use a "for" loop inside children of a widget?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM