flutter_lazy_loader 1.0.3 icon indicating copy to clipboard operation
flutter_lazy_loader: ^1.0.3 copied to clipboard

A lazily loaded list for flutter. Allows for loading data in small chunks at a time instead of everything at once.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_lazy_loader/flutter_lazy_loader.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Lazy loading list demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FlutterLazyLoaderExample(),
    );
  }
}

class FlutterLazyLoaderExample extends StatefulWidget {
  FlutterLazyLoaderExample({Key? key}) : super(key: key);

  final List<int> list = List.generate(100, (index) => index);

  @override
  State<FlutterLazyLoaderExample> createState() =>
      _FlutterLazyLoaderExampleState();
}

class _FlutterLazyLoaderExampleState extends State<FlutterLazyLoaderExample> {
  int _lastIndex = 0;

  Future<List<int>> _dataloader() {
    if (_lastIndex >= widget.list.length) {
      // Returning empty list if we are out of data
      return Future.value(List.empty());
    }

    var _nextIndex = _lastIndex + 5 < widget.list.length
        ? _lastIndex + 5
        : widget.list.length - 1;

    var newListItems = widget.list.getRange(_lastIndex, _nextIndex).toList();
    _lastIndex += 5;

    // Otherwise return the next batch of data
    return Future.delayed(const Duration(seconds: 1))
        .then((value) => newListItems);
  }

  Widget _listTile(int data) {
    return ListTile(
      leading: Text(data.toString()),
      title: Text("A list tile from data $data"),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("List example"),
      ),
      body: Center(
        child: FlutterLazyLoader<int>(
          dataLoader: _dataloader,
          listTile: _listTile,
        ),
      ),
    );
  }
}
2
likes
130
pub points
18%
popularity

Publisher

unverified uploader

A lazily loaded list for flutter. Allows for loading data in small chunks at a time instead of everything at once.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_lazy_loader