首页 > 解决方案 > Flutter Assertion 调用函数失败

问题描述

我有一个简单的身体,GestureDetector 工作正常。我尝试在正文中添加功能,但其显示断言失败。

class AlphabetPage extends StatefulWidget {
  @override
  _AlphabetPageState createState() => _AlphabetPageState();
}

class _AlphabetPageState extends State<AlphabetPage> {


  testing(){
    GestureDetector(
      onTap: (){
        play('a');
      },
      child: Container(

        color: Colors.white,
        margin: EdgeInsets.fromLTRB(15, 10, 0, 0),
        child:  Image(image: AssetImage('images/a.jpg'),
          height: 60,
          width: 60,

        ),

      ),
    );
  }
   play(a){
    final player = AudioCache();
    player.play('$a.wav');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.purple,
          automaticallyImplyLeading: true,
          //`true` if you want Flutter to automatically add Back Button when needed,
          //or `false` if you want to force your own back button every where
          title: Text('Alphabet Sounds'),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () => Navigator.pop(context, false),
          ),
        ),
          body: Column(children: <Widget>[
            Row(
              //ROW 1
              children: [
                     testing(),
                     testing(),
              ],
            ),
          ],
          ),
      ),
      );

  }
}

如果直接在正文中使用测试功能,当我将其包装在测试功能中并在正文中调用测试时,它的工作正常。

标签: flutterdart

解决方案


更新你testing

Widget testing() {
  return GestureDetector(
    onTap: () {
      play('a');
    },
    child: Container(
      color: Colors.white,
      margin: EdgeInsets.fromLTRB(15, 10, 0, 0),
      child: Image(
        image: AssetImage('images/a.jpg'),
        height: 60,
        width: 60,
      ),
    ),
  );
}

推荐阅读