Dart Flutter:功能未执行
Dart Flutter: Function not executing
我是 Dart 和 Flutter 的新手,我正在尝试测试 Future
功能。我写了一个简短的 async
函数,其中嵌套了两个 Future
对象。
问题: 函数不执行,当我尝试将其归因于变量时出现错误 This expression has a type of 'void' so its value can't be used.
和 Only static members can be accessed in initializers.
。
这是代码:[![在此处输入图片描述][1]][1]
@override
_ChoseLocationState createState() => _ChoseLocationState();
}
class _ChoseLocationState extends State<ChoseLocation> {
int counter = 0;
void simulateRequest() async {
// first future holds family name
String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
String famName = 'Shanshi';
return famName;
});
// second future holds first name
String compName = await Future.delayed(Duration(seconds: 1), (){
String fstName = 'Yoshi';
String compName = '$fstName - $famNameFunc';
return compName;
});
print(compName);
}
dynamic funcex = simulateRequest();
@override
void initState() {
super.initState();
print('This is the initial state.');
}
@override
Widget build(BuildContext context) {
print('This is the build function processing.');
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Set Location'),
centerTitle: true,
),
body: RaisedButton(
onPressed: (){
setState(() {
counter++;
});
},
color: Colors.blue,
child: Text('Counter is: $counter'),
),
);
}
}```
[1]: https://i.stack.imgur.com/XmvY9.png
尝试以下操作:
class ChoseLocation extends StatefulWidget {
ChoseLocation({Key key, this.title}) : super(key: key);
final String title;
@override
_ChoseLocationState createState() => _ChoseLocationState();
}
class _ChoseLocationState extends State<ChoseLocation> {
int counter = 0;
void simulateRequest() async {
// first future holds family name
String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
String famName = 'Shanshi';
return famName;
});
// second future holds first name
String compName = await Future.delayed(Duration(seconds: 1), (){
String fstName = 'Yoshi';
String compName = '$fstName - $famNameFunc';
return compName;
});
print(compName);
}
// dynamic funcex = simulateRequest();
@override
void initState() {
super.initState();
simulateRequest();
print('This is the initial state.');
}
@override
Widget build(BuildContext context) {
print('This is the build function processing.');
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Set Location'),
centerTitle: true,
),
body: RaisedButton(
onPressed: (){
setState(() {
counter++;
});
},
color: Colors.blue,
child: Text('Counter is: $counter'),
),
);
}
}
您需要在方法示例 initState
中调用 simulateRequest()
,并且由于它没有 return 任何内容,因此您无法将其分配给变量。
simulateRequest
有一个 return 类型的 void
,所以如果你想把这个函数存储在变量中,你不应该加上括号。如果您使用括号,您将成为 运行 函数并将其 returned 值分配给变量 funcex
,这就是为什么您得到:This expression has a type of 'void' so its value can't be used.
@override
void initState() {
dynamic funcex = simulateRequest;
funcex();
super.initState();
print('This is the initial state.');
}
我是 Dart 和 Flutter 的新手,我正在尝试测试 Future
功能。我写了一个简短的 async
函数,其中嵌套了两个 Future
对象。
问题: 函数不执行,当我尝试将其归因于变量时出现错误 This expression has a type of 'void' so its value can't be used.
和 Only static members can be accessed in initializers.
。
这是代码:[![在此处输入图片描述][1]][1]
@override
_ChoseLocationState createState() => _ChoseLocationState();
}
class _ChoseLocationState extends State<ChoseLocation> {
int counter = 0;
void simulateRequest() async {
// first future holds family name
String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
String famName = 'Shanshi';
return famName;
});
// second future holds first name
String compName = await Future.delayed(Duration(seconds: 1), (){
String fstName = 'Yoshi';
String compName = '$fstName - $famNameFunc';
return compName;
});
print(compName);
}
dynamic funcex = simulateRequest();
@override
void initState() {
super.initState();
print('This is the initial state.');
}
@override
Widget build(BuildContext context) {
print('This is the build function processing.');
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Set Location'),
centerTitle: true,
),
body: RaisedButton(
onPressed: (){
setState(() {
counter++;
});
},
color: Colors.blue,
child: Text('Counter is: $counter'),
),
);
}
}```
[1]: https://i.stack.imgur.com/XmvY9.png
尝试以下操作:
class ChoseLocation extends StatefulWidget {
ChoseLocation({Key key, this.title}) : super(key: key);
final String title;
@override
_ChoseLocationState createState() => _ChoseLocationState();
}
class _ChoseLocationState extends State<ChoseLocation> {
int counter = 0;
void simulateRequest() async {
// first future holds family name
String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
String famName = 'Shanshi';
return famName;
});
// second future holds first name
String compName = await Future.delayed(Duration(seconds: 1), (){
String fstName = 'Yoshi';
String compName = '$fstName - $famNameFunc';
return compName;
});
print(compName);
}
// dynamic funcex = simulateRequest();
@override
void initState() {
super.initState();
simulateRequest();
print('This is the initial state.');
}
@override
Widget build(BuildContext context) {
print('This is the build function processing.');
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Set Location'),
centerTitle: true,
),
body: RaisedButton(
onPressed: (){
setState(() {
counter++;
});
},
color: Colors.blue,
child: Text('Counter is: $counter'),
),
);
}
}
您需要在方法示例 initState
中调用 simulateRequest()
,并且由于它没有 return 任何内容,因此您无法将其分配给变量。
simulateRequest
有一个 return 类型的 void
,所以如果你想把这个函数存储在变量中,你不应该加上括号。如果您使用括号,您将成为 运行 函数并将其 returned 值分配给变量 funcex
,这就是为什么您得到:This expression has a type of 'void' so its value can't be used.
@override
void initState() {
dynamic funcex = simulateRequest;
funcex();
super.initState();
print('This is the initial state.');
}