еще раз. Фьючерсы кажутся постоянным источником путаницы для меня.
Я пытаюсь использовать FutureBuilder на странице, но snapshot.data всегда имеет значение null, даже если я могу напечатать данные из DBprovider прямо перед его возвратом.
У меня есть класс CentreDetailScreen, который использует initState для создания будущего.
class _CentreDetailScreenState extends State<CentreDetailScreen> {
Future centreFuture;
@override
void initState() {
super.initState();
centreFuture = widget._centresBloc.getCentre(widget.centreReference);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: centreFuture,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return CircularProgressIndicator();
case ConnectionState.active:
case ConnectionState.waiting:
return CircularProgressIndicator();
case ConnectionState.done:
print('snapshot: ${snapshot.data}'); <-always null
return Container(...//build UI
В CentresBlo c У меня есть:
Future<ClimbingCentre> getCentre(String centreId) async {
print('BloC get Centre'); <-- this prints
var _centre = await ClimbDB.db.getCentre(centreId);
print('Bloc has got a centre $_centre') <-- this doesn't
return _centre;
}
, а затем DBprovider ClimbDB имеет:
Future<ClimbingCentre> getCentre(String centreId) async {
try {
var map = Map<String, dynamic>();
map['action'] = _GET_ONE_ACTION;
map['centreId'] = centreId;
final response = await http.post(
'http://xxx.xxx.xx.x/flutter/climbinside/centre.php',
body: map);
if (200 == response.statusCode) {
var centre = json.decode(response.body);
var toReturn =
centre.map((centre) => new ClimbingCentre.fromJson(centre));
print($toReturn); <-prints 'instance of ClimbingCentre'
return toReturn;
} else {
throw Exception('we were not able to download json data');
}
} catch (e) {
throw Exception('Get centre: unable to download JSON');
}
}
Я перепробовал много разных вещей ... иногда я получаю ошибки о том, что Future не является подтипом Future ... другие вызывают исключение 'Unable Download JSON' ... Я не могу понять все это.
Буду очень признателен за любую помощь.