为什么我在运行此代码时遇到异常?

问题描述 投票:0回答:1

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

flutter flutter-layout flutter-dependencies
1个回答
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),
        );
      }),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.