Flutter ListView.builder not updating after insert - dart

I've got a SQLite database that I query data from. From such data I create the necessary widgets and store them in a list. Passing the list to ListView.builder I create the items from the list. Everything looks good except when I add new data to the widget list. Nothing shows up if I use insert. If I use add there's no issue. Here's the snippet.
List<MessageItem> _messageItems = <MessageItem>[];
// Reads the data and creates the widgets (all OK here)
// Called when reading the data <<<
_readDBMessages() async {
List<Map> messages = await readMessages(_threadID);
List<MessageItem> holderList = <MessageItem>[];
for (final msg in messages) {
holderList.add(new MessageItem(
msg['message'],
msg['timestamp'],
msg['status'],
_contactData['photo']));
}
_messageItems.clear();
setState(() {
_messages = msges;
_messageItems = holderList;
});
}
// When I use _messageItems.add(new MessageItem()); the item shows as expected but it
// it's located on top of the list. Since my ListView is reverse
// I instead use _messagesInsert(0, new MessageItem()); in doing so
// The list is not updated. Scrolling up/down will than proceed to
// show the item as expected.
// Called when storing the data <<<
_storeNewMessage(String message) {
int timestamp = new DateTime.now().millisecondsSinceEpoch;
storeMessage(_threadID, message, _me, 'sending', timestamp, 'text').then((onValue) {
// _readDBMessages();
setState(() {
_messageItems.add(
new MessageItem(
message, timestamp, 'sending', null
)
);
print('Message inserted ---------------');
});
});
}
// Here's my listView constructor <<<
new Expanded(
child: new ListView.builder(
reverse: true,
padding: const EdgeInsets.all(12.0),
itemCount: (_messageItems == null) ? 0 : _messageItems.length,
itemBuilder: (context, i) => _messageItems[i]
)
),
According to Flutter doctor everything is OK, Thanks for the help ;-)

Perhaps you should pass a unique key to your MessageItem.
When you receive a data:
new MessageItem(
msg['message'],
msg['timestamp'],
msg['status'],
_contactData['photo'],
key: Key(msg['message']+msg['timestamp'].toString())
)
When you add a new entry:
_messageItems.add(
new MessageItem(
message, timestamp, 'sending', null, key: Key("${message}${timestamp}")
)
);
In MessageItem constructor:
MessageItem(..., {Key key}): super(key: key);
Another way is to specify a random key for the list, something like this:
child: new ListView.builder(
key: new Key(randomString()),
You can read about the keys in https://flutter.io/widgets-intro/#keys or check Dismissible widget as an example

I had a similar problem where I was listening to Floor db changes with StreamBuilder, in which the StreamBuilder was reacting but ListView was not getting updated. Tried the above solution
child: new ListView.builder(
key: UniqueKey(),
The ListView was then getting updated with new data but there was one problem, in the above implementation the ListView gets refreshed which works great with new data additions, but when I update a data in db or remove a data using dismissible widget, the ListView is again refreshed and the first element is displayed.
So I tried adding the key to each widget in the itemBuilder and it worked for me
ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: array.length,
itemBuilder: (context, index) {
return ListItemWidget(
dao: dao,
item: array[index],
key: UniqueKey(),
);
},
);

Related

How to update other widget after future builder get data

I want to update the header widget after the future builder bellow it gets data from DB.
Consider this simplified code
Column (
children: [
Text('Data is loading'), // I want to changed it into `Data is loaded`
FutureBuilder(
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
if (!isCacheInitialized) {
_loadDB(); // get data from db
isCacheInitialized = true;
}
return ListView.builder
},
future: storage.ready,
)
]
)
What I already tried but failed
Call setState after _loadDB
result in error
setState() called during build.
So how to change the text Data is loading into Data is loaded after FutureBuilder gets the data?
Call setState inside the _loadDB() function instead. So, before the _loadDB() function hands over to Widget Build, It would have set the data you want.

Stream inside a Stream using Providers

So I have created a BLOC structure with a Stream as given below. The Fetcher would receive changes to a list of Chatroom ids. Then using the transformer, it would add the data in the stream to a Cache map and pipe it to the output.
Now the catch here is that each Chatroom IDs will be used to create a stream instance, so subscribe to any changes in the Chatroom data. So the Cache map basically has the Chatroom ID mapped to its corresponding Stream. ChatRoomProvider is binds the bloc with the app.
class ChatRoomBloc {
// this is similar to the Streambuilder and Itemsbuilder we have in the Stories bloc
final _chatroomsFetcher = PublishSubject<String>();
final _chatroomsOutput =
BehaviorSubject<Map<String, Observable<ChatroomModel>>>();
// Getter to Stream
Observable<Map<String, Observable<ChatroomModel>>> get chatroomStream =>
_chatroomsOutput.stream;
ChatRoomBloc() {
chatRoomPath.listen((chatrooms) => chatrooms.documents
.forEach((f) => _chatroomsFetcher.sink.add(f.documentID)));
_chatroomsFetcher.stream
.transform(_chatroomsTransformer())
.pipe(_chatroomsOutput);
}
ScanStreamTransformer<String, Map<String, Observable<ChatroomModel>>>
_chatroomsTransformer() {
return ScanStreamTransformer(
(Map<String, Observable<ChatroomModel>> cache, String id, index) {
// adding the iteam to cache map
cache[id] = chatRoomInfo(id);
print('cache ${cache.toString()}');
return cache;
}, <String, Observable<ChatroomModel>>{});
}
dispose() {
_chatroomsFetcher.close();
_chatroomsOutput.close();
}
}
Observable<ChatroomModel> chatRoomInfo(String _chatrooms) {
final _chatroomInfo = PublishSubject<ChatroomModel>();
Firestore.instance
.collection('chatRooms')
.document(_chatrooms)
.snapshots()
.listen((chatroomInfo) =>
_chatroomInfo.sink.add(ChatroomModel.fromJson(chatroomInfo.data)));
dispose() {
_chatroomInfo.close();
}
return _chatroomInfo.stream;
}
Then I create a Streambuilder with a List view to list the IDs and any data from their corresponding streams as given below.
class FeedList extends StatelessWidget {
#override
Widget build(BuildContext context) {
final chatroomBloc = ChatRoomProvider.of(context);
return Scaffold(
appBar: AppBar(
title: Text('Chat Room'),
),
body: buildList(chatroomBloc),
);
}
Widget buildList(ChatRoomBloc chatroomBloc) {
return StreamBuilder(
// Stream only top ids to display
stream: chatroomBloc.chatroomStream,
builder: (context,
AsyncSnapshot<Map<String, Observable<ChatroomModel>>> snapshot) {
if (!snapshot.hasData) { // no data yet
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, int index) {
print('index $index and ${snapshot.data}');
return buildTile(snapshot.data[index]);
},
);
});
}
Widget buildTile(Observable<ChatroomModel> chatroomInfoStream) {
return StreamBuilder(
stream: chatroomInfoStream,
builder: (context, AsyncSnapshot<ChatroomModel> chatroomSnapshot) {
if (!chatroomSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
print('${chatroomSnapshot.data.name}');
print('${chatroomSnapshot.data.members.toString()}');
return Column(children: [
ListTile(
title: Text('${chatroomSnapshot.data.name}'),
trailing: Column(
children: <Widget>[
Icon(Icons.comment),
],
),
),
Divider(
height: 8.0,
),
]);
});
}
}
The output I am getting is given below. The Streambuilder is stuck at CircularProgressIndicator in the buildTile method. I think it means that the instances are getting created and added in the cache map, but they are lot listening to the right instances or there is something wrong in the way I wired up the streams. Can you please help ?
I/flutter (12856): cache {H8j0EHhu2QpicgFDGXYZ: Instance of 'PublishSubject<ChatroomModel>'}
I/flutter (12856): cache {H8j0EHhu2QpicgFDGXYZ: Instance of 'PublishSubject<ChatroomModel>', QAhKYk1cfoq8N8O6WY2N: Instance of 'PublishSubject<ChatroomModel>'}
I/flutter (12856): index 0 and {H8j0EHhu2QpicgFDGXYZ: Instance of 'PublishSubject<ChatroomModel>', QAhKYk1cfoq8N8O6WY2N: Instance of 'PublishSubject<ChatroomModel>'}
I/flutter (12856): index 1 and {H8j0EHhu2QpicgFDGXYZ: Instance of 'PublishSubject<ChatroomModel>', QAhKYk1cfoq8N8O6WY2N: Instance of 'PublishSubject<ChatroomModel>'}
As a quick fix, maybe try:
final _chatroomInfo = BehaviorSubject<ChatroomModel>();
On a second note:
The code in its current state is hard to read and understand, it's unmaintainable and inefficient. I'm not sure what you are actually trying to do.
It's a bad idea to nest StreamBuilders. It will delay the display of the chat list by at least 2 frames, because every StreamBuilder renders at least one empty frame (data = null).
Listening to a stream and feeding the result into a Subject will also add delays.
If possible, try to remove all subjects. Instead, use rx operators.
The BLoC should provide a single output stream that provides all the data that is required to render the chat list.

Dart/Flutter - Flutter - Why ListView is going infinite

Not sure since I have just started building things with Flutter and Dart. If anyone can take a look at the code and can share inputs on:
How to display listview having fixed number of items, lets say in my example we are fetching 100 items
How to implement paging, lets say initially I want to fetch 1st page and then while scrolling page 2nd and so on.
Issues:
In current implementation, I am finding 2 issues:
Able to scroll endlessly at bottom
Finding exception in logcat output:
03-15 06:14:36.464 3938-3968/com.technotalkative.flutterfriends I/flutter: Another exception was thrown: RangeError (index): Invalid value: Not in range 0..99, inclusive: 100
I have posted the same issue on my Github repository: https://github.com/PareshMayani/Flutter-Friends/issues/1
Would appreciate if you contribute to this repo!
That is beacuse you are using ListView.builder which actually renders an infinite list when itemCount is not specified. Try specifying itemCount to 100.
For pagination, the simplest solution with a ListView.builder would be to show a refresh widget when the list has reached its end and initiate a refresh api call, and then add new items to the list and increase item count.
Example:
class Example extends StatefulWidget {
#override
_ExampleState createState() => new _ExampleState();
}
class _ExampleState extends State<Example> {
// initial item count, in your case `_itemCount = _friendList.length` initially
int _itemCount = 10;
void _refreshFriendList() {
debugPrint("List Reached End - Refreshing");
// Make api call to fetch new data
new Future<dynamic>.delayed(new Duration(seconds: 5)).then((_){
// after new data received
// add the new data to the existing list
setState(() {
_itemCount = _itemCount + 10; // update the item count to notify newly added friend list
// in your case `_itemCount = _friendList.length` or `_itemCount = _itemCount + newData.length`
});
});
}
// Function that initiates a refresh and returns a CircularProgressIndicator - Call when list reaches its end
Widget _reachedEnd(){
_refreshFriendList();
return const Padding(
padding: const EdgeInsets.all(20.0),
child: const Center(
child: const CircularProgressIndicator(),
),
);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
// ListView Builder
body: new ListView.builder(
itemCount: _itemCount + 1,
itemBuilder: (_, index) {
final Widget listTile = index == _itemCount // check if the list has reached its end, if reached end initiate refresh and return refresh indicator
? _reachedEnd() // Initiate refresh and get Refresh Widget
: new Container(
height: 50.0,
color: Colors.primaries[index%Colors.primaries.length],
);
return listTile;
},
),
);
}
}
Hope that helps!
Note: I'm not claiming this is the best way or is optimal but this is one of the ways of doing it. There is an example social networking app of git which does it in a different way, you can take a look at it here.

Flutter listview with Map instead of List

I'm trying to display the contents of my HashMap values in a ListView.builder widget. Is there a way to do this? With a List I could simply use the index, but how would that work with a HashMap without making a List out of it?
The keys of the map are strings and the values are maps with the data to display.
Its a little late but You could also try this.
Map values = snapshot.data;
return new ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
String key = values.keys.elementAt(index);
return new Column(
children: <Widget>[
new ListTile(
title: new Text("$key"),
subtitle: new Text("${values[key]}"),
),
new Divider(
height: 2.0,
),
],
);
},
);
for a more detailed example check this out
https://kodestat.gitbook.io/flutter/39-flutter-listviewbuilder-using-dart-maps
Just make a list from the keys and then get the value using the index to get the map key and use it to get the map value
var keys = myMap.keys.toList();
var val = myMap[keys[idx]];
it is possible, you can do something like this
map.forEach((key, value) {
// here you can write your logic using "Value object",
// make new object of your list view item and
// add it to it's builder list using
setState(() {
_builderList.insert(0, itemObject);
});
});
or you can try
final list = map.values.toList(growable: {true/false});
// play with your list
Just use inside the listview builder yourlistname[index ]['keyname' ], that works for me.

How do I use async/HTTP data to return child widgets in an IndexedWidgetBuilder?

I'm obtaining JSON data over HTTP and displaying it in a ListView. Since it's HTTP, it's all async.
Here's what I'd like to do:
var index = new ListView.builder(
controller: _scrollController,
itemBuilder: (ctx, i) async {
_log.fine("loading post $i");
var p = await _posts[i];
return p == null ? new PostPreview(p) : null;
},
);
Unfortunately, this doesn't work since IndexedWidgetBuilder has to be a synchronous function. How do I use a Future to build a child for an IndexedWidgetBuilder? It doesn't seem like there's a way to wait for the future to complete synchronously.
Previously, I was loading the data into an array and the IndexedWidgetBuilder function only checked to see if the list elements existed before returning the child widget.
var index = new ListView.builder(
controller: _scrollController,
itemBuilder: (ctx, i) {
_log.fine("loading post $i");
return _posts.length > i ? new PostPreview(_posts[i]) : null;
},
);
This works, but I would like to completely separate the view from the data and asynchronously request the JSON as needed.
This also seems, in my limited experience, like it might be a common use-case. Could an async version of IndexWidgetBuilder be added to flutter?
You can wait for asynchronous computations using a FutureBuilder. I'd probably change your PostPreview to take a Future as a constructor argument and put the FutureBuilder there, but if you want to leave PostPreview as-is, here's how to modify your itemBuilder.
var index = new ListView.builder(
controller: _scrollController,
itemBuilder: (ctx, i) {
return new FutureBuilder(
future: _posts[i],
builder: (context, snapshot) {
return snapshot.connectionState == ConnectionState.done
? new PostPreview(snapshot.data)
: new Container(); // maybe a show placeholder widget?
);
},
);
The nice thing about FutureBuilder is that it takes care of the scenarios where the async request completes and your State has already been disposed.

Resources