默认 'List' 构造函数不可用
The default 'List' constructor isn't available
为什么flutter不能使用默认的List构造函数?
每次我使用这个列表构造函数时它都会抛出一个错误
(错误:错误:无法使用默认的 List 构造函数。请尝试使用 List.filled。)
错误行: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(),
)
],
),
)
),
);
}
}
请帮助我更改代码
( List _items = new List(); ) 和 ( List _items = (); )
它消除了错误,但没有在屏幕上显示任何内容
问题是您使用的是行而不是列。
试试这个代码
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(),
)
],
),
)
),
)
);
}
}
为什么flutter不能使用默认的List构造函数?
每次我使用这个列表构造函数时它都会抛出一个错误 (错误:错误:无法使用默认的 List 构造函数。请尝试使用 List.filled。)
错误行: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(),
)
],
),
)
),
);
}
}
请帮助我更改代码 ( List _items = new List(); ) 和 ( List _items = (); ) 它消除了错误,但没有在屏幕上显示任何内容
问题是您使用的是行而不是列。
试试这个代码
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(),
)
],
),
)
),
)
);
}
}