Попробуйте следующее:
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'),
),
);
}
}
Вам нужно вызвать simulateRequest()
внутри примера метода initState
, и, поскольку он ничего не возвращает, вы не можете присвоить его переменной.