0

Why flutter can't use the default List constructor?

Every time i use this List constructor it throws an error (Error: Error: Can't use the default List constructor. Try using List.filled instead.)

ErrorLine: List _items = new List();

import 'package:flutter/material.dart';
void main() {
 runApp(new MaterialApp(
   home: new MyApp(),
 ));
}

class MyApp extends StatefulWidget {
 @override
 _State createState() => new _State();
}

class MyItem {
 bool isExpanded;
 final String header;
 final Widget body;
  MyItem(this.isExpanded, this.header, this.body);
}

class _State extends State<MyApp> {
 List<MyItem> _items = new List<MyItem>();

@override
void initState() {
for (int i =0; i< 10; i++){
  _items.add(new MyItem(
    false,
    'Item ${i}',
    new Container(
      padding: new EdgeInsets.all(10.0),
      child: new Text('Hello World'),
    )
  ));
 }
}

ExpansionPanel _createitem(MyItem item) {
return new ExpansionPanel(
    headerBuilder: (BuildContext context, bool isExpanded) {
      return new Container(
        padding: new EdgeInsets.all(5.0),
        child: new Text('Header ${item.header}'),
      );
    },
    body: item.body,
    isExpanded: item.isExpanded
);
}

@override
Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    title: new Text('Expansion Panel'),
  ),
  body: new Container(
      padding: new EdgeInsets.all(32.0),
      child: new Center(
        child: new Row(
          children: <Widget>[
            new ExpansionPanelList(
              expansionCallback: (int index, bool isExpanded) {
                setState(() {
                  _items[index].isExpanded = !_items[index].isExpanded;
                });
              },
              children: _items.map(_createitem).toList(),
            )
          ],
        ),
      )
  ),
);
}


}

Please help Me out I change the code ( List _items = new List(); ) with ( List _items = (); ) It remove the error but didnot show anything on screen

Anuj Verma
  • 11
  • 4

1 Answers1

0

The issue is you are using Row instead of column.

Try this code

import 'package:flutter/material.dart';
void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => new _State();
}

class MyItem {
  bool isExpanded;
  final String header;
  final Widget body;
  MyItem(this.isExpanded, this.header, this.body);
}

class _State extends State<MyApp> {
  List<MyItem> _items = [];

  @override
  void initState() {
    for (int i =0; i< 10; i++){
      _items.add(new MyItem(
          false,
          'Item $i',
          new Container(
            padding: new EdgeInsets.all(10.0),
            child: new Text('Hello World'),
          )
      ));
    }
  }

  ExpansionPanel _createitem(MyItem item) {
    return new ExpansionPanel(
        headerBuilder: (BuildContext context, bool isExpanded) {
          return new Container(
            padding: new EdgeInsets.all(5.0),
            child: new Text('Header ${item.header}'),
          );
        },
        body: item.body,
        isExpanded: item.isExpanded
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Expansion Panel'),
      ),
      body: SingleChildScrollView(
        child: Container(
 
            child: new Center(
              child: new Column(
                children: <Widget>[
                  new ExpansionPanelList(
                    expansionCallback: (int index, bool isExpanded) {
                      setState(() {
                        _items[index].isExpanded = !_items[index].isExpanded;
                      });
                    },
                    children: _items.map(_createitem).toList(),
                  )
                ],
              ),
            )
        ),
      )
    );
  }


}
Muraino
  • 554
  • 7
  • 18