为什么我在 运行 这段代码时出现异常?

Why i am getting Exception while running this code?

我是 Flutter 新手。我收到这个奇怪的异常。我尝试更改代码,删除 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),
         );
        }
     ),

   ) ;
  }
}

it is telling me that setstate() method should not be called because framework is already running. I removed this method but no change. Please help.

此代码有多个错误:

  1. 你 return ScaffoldAppRandomwords 中。您可能只能使用一次。
  2. 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),
        );
      }),
    );
  }
}