gpt4 book ai didi

调用函数时 Flutter 断言失败

转载 作者:IT王子 更新时间:2023-10-29 07:15:22 26 4
gpt4 key购买 nike

我有一个带有 GestureDetector 的简单主体,它工作正常。我尝试在 body 中添加函数,但它显示断言失败。

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(),
],
),
],
),
),
);

}
}

如果我直接在 body 中使用测试函数,当我将它包装在测试函数中并在 body 中调用测试时,它工作正常,它显示错误。

最佳答案

将您的测试更新为

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,
),
),
);
}

关于调用函数时 Flutter 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com