为什么在运行此代码时出现异常?

时间:2019-03-03 15:26:35

标签: flutter flutter-layout flutter-dependencies

我是新手。我收到这个奇怪的例外。我尝试更改代码,删除了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()方法,因为   框架已经在运行。我删除了此方法,但没有更改。   请帮忙。

1 个答案:

答案 0 :(得分:0)

此代码有多个错误:

  1. 您在ScaffoldApp中返回Randomwords。您可能只能使用一次。
  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),
        );
      }),
    );
  }
}