I am a beginner at programming and try to code an App in Flutter. I load data from algolia to StreamBuilder (by using an algolia lib in combination with as.Stream() - otherwise it doesnt work). The code works well, but it does not automatically refresh the data - I have to load a new screen in the app or hot reload, then the data updates. Looks like the StatefulWidget behaves like a StatelessWidget. Do you have any idea - maybe include setState somewhere (which I thought isn't necessary for StreamBuilder)? Thanks for your help ;-).
import 'package:first_app/group_detail.dart';
import 'package:flutter/material.dart';
import 'package:algolia/algolia.dart';
class First extends StatefulWidget {
@override
FirstState createState() {
return new FirstState();
}
}
class FirstState extends State<First> {
static Algolia algolia = Algolia.init(
applicationId: 'xxx',
apiKey: 'xxx',
);
queryFunc() {
AlgoliaQuery query = algolia.instance.index('places).setAroundLatLng('51.5078845,7.4702625');
Future<AlgoliaQuerySnapshot> snap = query.getObjects();
return snap;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return StreamBuilder(
stream: queryFunc().asStream(),
builder:
(BuildContext context, AsyncSnapshot<AlgoliaQuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
final int documentsLength = snapshot.data.hits.length;
return new ListView.builder(
itemCount: documentsLength,
itemBuilder: (context, int index) {
final AlgoliaObjectSnapshot document = snapshot.data.hits[index];
return new ListTile(
leading: CircleAvatar(
child: Text(document.data['name'].substring(0, 1))),
title: new Text(document.data['name']),
subtitle: new Text(document.data['text]),
);
});
},
);
}
}