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

Viewed 46

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

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.

Related