我是新来的人。我得到了这个奇怪的例外。我尝试更改代码,删除了setstate()方法,但仍有异常。
代码如下:
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
main()=>runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text('superliker',
style: TextStyle(color: Colors.teal,
fontSize: 25,
fontFamily: 'Roboto'
),
),
),
body: new Center(
child: new Randomwords(),
),
),
);
}
}
class Randomwords extends StatefulWidget{
@override
Randomwordstate createState()=>new Randomwordstate();
}
class Randomwordstate extends State<Randomwords>{
final List<WordPair> _suggestions=<WordPair>[];
final Set<WordPair> _saved=new Set<WordPair>();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
actions: <Widget>[
new IconButton(icon: Icon(Icons.list), onPressed: _pushsaved(),),
],
),
body: _buildsuggestions(),
);
}
Widget _buildsuggestions(){
return new ListView.builder(
padding: EdgeInsets.all(16),
itemBuilder: (BuildContext _context, int i){
if (i.isOdd){
return Divider();
}
final int index=i~/2;
if( index>=_suggestions.length){
_suggestions.addAll(generateWordPairs().take(10));
}
return _Buildrow(_suggestions[index]);
}
);
}
Widget _Buildrow(WordPair pair){
final bool alreadysaved=_saved.contains(pair);
return ListTile(
title: Text(pair.asPascalCase,
style: TextStyle(
fontSize: 16,
),
),
trailing:new Icon(
alreadysaved ?Icons.favorite:Icons.favorite_border,
color: alreadysaved ? Colors.red:null,
),
onTap: (){
setState(() {
if (alreadysaved) {
_saved.remove(pair);
}
else
_saved.add(pair);
});
}
);
}
_pushsaved(){
Navigator.of(context).push(
new MaterialPageRoute<void>(
builder: (BuildContext context){
final Iterable<ListTile> tiles= _saved.map(
(WordPair pair){
return new ListTile(
title: Text(pair.asPascalCase,
style: TextStyle(
fontSize: 16,
),),
);
},
);
final List<Widget> divided= ListTile.divideTiles(
context: context,
tiles: tiles,
)
.toList();
return new Scaffold(
appBar: AppBar(
title: Text('saved words',
style: TextStyle(color: Colors.teal,
fontSize: 25,
fontFamily: 'Roboto'
),
),
),
body: new ListView(children: divided),
);
}
),
) ;
}
}
它告诉我不应该调用setstate()方法,因为框架已经在运行。我删除了这个方法但没有改变。请帮忙。
此代码有多个错误:
Scaffold
和App
返回Randomwords
。您可能只需使用一次。onPressed: _pushsaved(),
- 在这一行你调用这个函数,而不是你应该通过它,像这样:onPressed: _pushsaved,
完整的工作示例:
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: new Randomwords());
}
}
class Randomwords extends StatefulWidget {
@override
Randomwordstate createState() => new Randomwordstate();
}
class Randomwordstate extends State<Randomwords> {
final List<WordPair> _suggestions = <WordPair>[];
final Set<WordPair> _saved = new Set<WordPair>();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
actions: <Widget>[
new IconButton(
icon: Icon(Icons.list),
onPressed: _pushsaved,
),
],
),
body: _buildsuggestions(),
);
}
Widget _buildsuggestions() {
return new ListView.builder(
padding: EdgeInsets.all(16),
itemBuilder: (BuildContext _context, int i) {
if (i.isOdd) {
return Divider();
}
final int index = i ~/ 2;
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
return _Buildrow(_suggestions[index]);
});
}
Widget _Buildrow(WordPair pair) {
final bool alreadysaved = _saved.contains(pair);
return ListTile(
title: Text(
pair.asPascalCase,
style: TextStyle(
fontSize: 16,
),
),
trailing: new Icon(
alreadysaved ? Icons.favorite : Icons.favorite_border,
color: alreadysaved ? Colors.red : null,
),
onTap: () {
setState(() {
if (alreadysaved) {
_saved.remove(pair);
} else
_saved.add(pair);
});
});
}
void _pushsaved() {
Navigator.of(context).push(
new MaterialPageRoute<void>(builder: (BuildContext context) {
final Iterable<ListTile> tiles = _saved.map(
(WordPair pair) {
return new ListTile(
title: Text(
pair.asPascalCase,
style: TextStyle(
fontSize: 16,
),
),
);
},
);
final List<Widget> divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();
return new Scaffold(
appBar: AppBar(
title: Text(
'saved words',
style: TextStyle(
color: Colors.teal, fontSize: 25, fontFamily: 'Roboto'),
),
),
body: new ListView(children: divided),
);
}),
);
}
}