How to make this containers in flutter depending on length from api? - flutter

I try to make this column but data come form API, so i need solve to this problem ?

You can used Listview with builder
ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
ProjectModel project = snapshot.data[index];
return Column(
children: <Widget>[
// Widget to display the list of project
],
);
},
);
here snapshot.data is coming from future API for an example if your API gives list of object you can use FutureBuilder to wait until request is complete. And fetch snapshot data to ListView.builder here docs about future builder you can find clear idea from this

Related

Flutter - what is the best way to load 10,000 more of the list of sentences from json file

I have a json file which have more than 10,000 list of sentences. I want to know what is the effective way when I click a button, it will redirect to a page and load the list of 10,000 sentences without slowing the performance or crash the memory.
Below are the sample of my code:
class _PageLoadState extends State<PageLoadState> {
Future<BookModel> getSentencesList() async {
final response = await rootBundle.loadString('assets/books.json');
var data = jsonDecode(response);
return BookModel.fromJson(data);
}
// This is the body section
Expanded(
child: FutureBuilder<BookModel>(
future: getSentencesList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) => Wrap(
children: [
Padding(
padding: EdgeInsets.all(18.0),
child: Text(
snapshot.data[index].sentence.toString(),
),
)
],
)
);
}
You can use lazy loading. Ex. You will just load 100 sentences and when the user scrolls and hits the bottom of the screen you will increment it by +100 and so on.
you can check this:
https://pub.dev/packages/lazy_load_scrollview
you can use the pagination for this. that easy to load a huge amount of list.
using pagination you can load the list as per requirements for example: first, you need to load only 100 items of a list, and when the user scrolls over the 100 items you can load more ,100 items.
and for that, you can use:- loadmore package
https://pub.dev/packages/loadmore
you can see the following example as a reference.

Need I do pagination for flutter StreamBuilder?

I'm new to flutter, and now I'm creating an app which has a feed page, I'm using StreamBuilder + firestore to do this, the code is like this:
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.orderBy('createdAt', descending: true)
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: primaryColor,
),
);
}
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => Container(
child: createPostViewFromSnapShot(snapshot.data!.docs[index]),// it returns a widget
),
);
},
);
You can see from the code I didn't do pagination, I want to know when the code runs, it fetch all the post from firestore ? Or it will fetch data by block or something like pagination ?
I want to know if it's necessary to do pagination for StreamBuilder?
Avoid creating Widgets in Methods. That way you cause your App to
become I'm-performant. Because the Widget is not sat directly in the
Tree, but a method, every build that methods gets called, no matter
if the resulting widget would have to be actually be rebuilt.
That stream will not always emit events where data is not null. You will most likely get an exception for using snapshot.data! bang operator
Take a closer look at the documentation: https://firebase.flutter.dev/docs/firestore/usage/
FlutterFire provides support for dealing with realtime changes to
collections and documents. A new event is provided on the initial
request, and any subsequent changes to collection/document whenever a
change occurs (modification, deleted or added).
For what you are trying to achieve, it would be better to initially fetch a limited set of documents from your collection.
collectionReference.limit(42).get()
You can than fetch the next elements by using https://firebase.flutter.dev/docs/firestore/usage/#start--end-cursors

How scroll down chat list view when open the chat Flutter

I would like to make my chat messages to scroll down every time I open a someone's chat. I have tried with jumpTo(), but i didn't worked out. Maybe someone have any clue how to make it work? Thank you in advance!
Here is the code snippet
Widget ChatMessageList() {
return StreamBuilder(
stream: chatMessageStream,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
controller: controller,
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
return MessageTile(
snapshot.data.docs[index].data()["message"],
snapshot.data.docs[index].data()["sendBy"] ==
Constants.myName,
);
controller.jumpTo(controller.position.maxScrollExtent);
},
)
: Container();},);}
ListView.builder() has a parameter named reverse. This is false by default, if you change it to true you probably achieve what you want to achieve.
ListView.builder(
reverse: true,
);
If your main goal is to get the latest messages first you can use Reverse for the Snapshot
final messages = snapshot.data.docs.reversed;
I hope you got the point

How to get access to a collection inside the start collection by using a StreamBuilder

I would like to know how can I get access to the fields of a collection that is inside the start collection? (see image below)
This is the streamBuilder I use to get access to the start(first) collection (posts) but I want a way to transform this streamBuilder so that I can get access to another collection inside the posts location. Thank you
StreamBuilder(
///IF YOU WANT TO CHANGE THE COLECCTION LIST THIS IS THE PLACE TO DO IT
stream: Firestore.instance.collection('posts').snapshots(),
builder: (context, snapshot) {
///THIS IS THE PLACE WHEN YOU CAN ADD AN ANIMATION
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemExtent: 100.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
Post post = Post.fromDoc(snapshot.data.documents[index]);
return _buildUserTile(post);
},
);
},[![enter image description here][1]][1]
),
I want to be able to get the data that is inside a colection inside the first colection (posts).
See below my firebase database
I am not sure but maybe you can get an idea from the below link:
https://groups.google.com/forum/#!topic/flutter-dev/ayIxrLiz1eQ

How to put divider between few of list items in ListView.separated

I want to put google ads after specific number of items list or in between the items list in ListView.separated.
I have attached an image to make it clear how I want to do it:
Source of the image is: this article
First, I believe, you need to install a library to display Google AdMob ads. For instance - admob_flutter lib.
Then you only need to set up the separatorBuilder property of your ListView.separated widget.
e.g.
ListView.separated(
itemCount: 25,
separatorBuilder: (BuildContext context, int index) {
if (index % 5 == 0) { // Display `AdmobBanner` every 5 'separators'.
return AdmobBanner(
adUnitId: /* AdMob Unit ID */,
adSize: AdmobBannerSize.BANNER,
);
}
return Divider();
},
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('item $index'),
);
},
)
This code is only an example, you might encounter Infinite Height layout errors as I am not aware of how admob_flutter widgets operate.
However, this should be enough to get you started with your idea.
Let me know if this helped.