1

I am receiving data from an API and displaying it in a specific page. I want to display the data dynamically, that when an user selects an object from a list view builder, it refresh the database automatically and removes it from the other user's screen. For that, I imagine StreamBuilder would be the best choice, based on this Multiuser application using flutter. I Hope someone can help me on that :)

Future:

class OrdersBloc {
  Future<List<OrdersModel>> getOrders() async {
    try {
      WooCommerceAPI wooCommerceAPI = WooCommerceAPI(
        url: urlWC,
        consumerKey: consumerKeyWC,
        consumerSecret: consumerSecretWC,
      );

      List<OrdersModel> ordersList = [];
      var listOrders = await wooCommerceAPI.getAsync(statusProcessing);

      for (var item in listOrders) {
        ordersList.add(OrdersModel.fromJson(item));
      }

      return ordersList;
    } catch (e) {
      print(e);
      return null;
    }
  }
}

FutureBuilder:

         FutureBuilder(
          future: OrdersBloc().getOrders(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.done:
                return ListView.builder(
                  scrollDirection: Axis.horizontal,
                  shrinkWrap: true,
                  padding: EdgeInsets.only(bottom: 10),
                  itemCount: snapshot.data != null ? snapshot.data.length : 0,
                  itemBuilder: (BuildContext context, int index) {
                    OrdersModel orderItem = snapshot.data[index];
                    return Container(...);

Thanks!!

cabral279
  • 97
  • 10

1 Answers1

1

To use a StreamBuilder, your data source must be a Stream, and to have a Stream as your data source your database should be a real-time database. You can't implement that feature with a database based on an HTTP request/response, your database should have a socket connection with all devices so it will be able to send data to all of them whenever the data are updated.

sitatech
  • 1,311
  • 11
  • 17