Noobdeveloper
Noobdeveloper

Reputation: 505

update list by new orderby string flutter firestore list

I want to update the orderby of the list on a click of a button with pagination in any kind of architecture tried setstate string but doesn't reload the list by new order by.

String? query = 'id';

class Model extends StatefulWidget {
  @override
  _ModelState createState() => _ModelState();
}

class _ModelState extends State<Model> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          TextButton(
            onPressed: () {
              setState(() {
                query = 'price';
              });
            },
            child: Text('data'),
          ),
          PaginateFirestore(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemBuilderType: PaginateBuilderType.listView,
            itemBuilder: (index, context, documentSnapshot) {
              final data = documentSnapshot.data() as Map?;
              return MainComponent(title: data == null ? 'Error' : data['id']);
            },
            // orderBy is compulsory to enable pagination
            query:
                FirebaseFirestore.instance.collection('phones').orderBy(query.toString()),
            // to fetch real-time data
            isLive: false,
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Views: 296

Answers (1)

Rohan Thacker
Rohan Thacker

Reputation: 6357

Add a key to PaginateFirestore, this should trigger a repaint of the PaginateFirestore widget.

PaginateFirestore(
key: ValueKey(query) // For Example
...
)

You can read more about keys here

Upvotes: 2

Related Questions