Flutter: StreamBuilder behaves like StatelessWidget












0















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









share|improve this question























  • Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

    – pblead26
    Jan 22 at 10:54













  • @pblead26 do you have an idea to solve this problelm?

    – Alexandra
    Jan 22 at 14:05
















0















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









share|improve this question























  • Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

    – pblead26
    Jan 22 at 10:54













  • @pblead26 do you have an idea to solve this problelm?

    – Alexandra
    Jan 22 at 14:05














0












0








0








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









share|improve this question














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






flutter algolia stream-builder






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 14:21









AlexandraAlexandra

61




61













  • Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

    – pblead26
    Jan 22 at 10:54













  • @pblead26 do you have an idea to solve this problelm?

    – Alexandra
    Jan 22 at 14:05



















  • Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

    – pblead26
    Jan 22 at 10:54













  • @pblead26 do you have an idea to solve this problelm?

    – Alexandra
    Jan 22 at 14:05

















Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

– pblead26
Jan 22 at 10:54







Look at this doc api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once.

– pblead26
Jan 22 at 10:54















@pblead26 do you have an idea to solve this problelm?

– Alexandra
Jan 22 at 14:05





@pblead26 do you have an idea to solve this problelm?

– Alexandra
Jan 22 at 14:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277391%2fflutter-streambuilder-behaves-like-statelesswidget%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277391%2fflutter-streambuilder-behaves-like-statelesswidget%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How fix org.hibernate.TransientPropertyValueException

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage