Using more than one table

Hi
I am quite new to flutter and I was hoping someone with a bit more experience could point me in the right direction.
I have used the example in the Real-Time Database Integration (Link to pull data from a Backendless table and then display it. It all works brilliantly and I have played around and made it all display how I’d like it to.

So in the example the Main.dart file creates a list of movies from the table ‘Movies’ and then displays them. Is it possible to do this a second time and display a second list?

In my example I’m working with travel data so I want a list of transfers at the top of the page from the ‘Transfers’ table and then then second list from the ‘Flights’ table.

I would appreciate any assistance or direction.
Thanks

Hello @Ross_Coombe1

Could you please provide a part of the code where you try to do it?

Regards, Dima.

Yeah sure
My code is identical to this with the exception of the table name and column names. This example displays one list from one table in Backendless. I’d like to be able to display a second list underneath it with data from a second table.

So this is the main.dart file

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

import 'movies.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     title: 'RT Database Demo',
     theme: ThemeData(
       primarySwatch: Colors.blue,
     ),
     home: MyHomePage(
       title: 'Watched Movies'
     ),
   );
 }
}

class MyHomePage extends StatefulWidget {
 MyHomePage({Key key, this.title}) : super(key: key);

 final String title;

 @override
 _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
 static const String API_HOST = "https://api.backendless.com";
 static const String APP_ID = "YOUR_APP_ID";
 static const String ANDROID_APP_KEY = "YOUR_ANDROID_API_KEY";
 static const String IOS_APP_KEY = "YOUR_IOS_API_KEY";
 IDataStore<Map> moviesStore = Backendless.data.of('Movies');
 List<Map> moviesList = [];

 @override
 void initState() {
   super.initState();
   _initBackendless();
   _enableRealTime();
   getMovies();
 }

 void _initBackendless() {
   Backendless.setUrl(API_HOST);
   Backendless.initApp(APP_ID, ANDROID_APP_KEY, IOS_APP_KEY);
 }

 void _enableRealTime() {
   // we will add code for Real Time Database here
 }

 void getMovies() {
   DataQueryBuilder queryBuilder = DataQueryBuilder()
     ..pageSize = 100
     ..sortBy = ['created'];

   moviesStore.find(queryBuilder).then((response) => setState(() => moviesList = response));
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text(widget.title),
     ),
     body: Movies(moviesList),
   );
 }
}

And this is the movies.dart file

    import 'package:flutter/material.dart';


    class Movies extends StatelessWidget {
     final List<Map> moviesList;

     Movies(this.moviesList);

     Widget _buildMovieItem(BuildContext context, int index) {
       return Column(
         crossAxisAlignment: CrossAxisAlignment.start,
         children: [
           Text(moviesList[index]['title'],
             style: TextStyle(
               color: Colors.black,
               fontSize: 16,
               fontWeight: FontWeight.bold,
             ),
           ),
           Text(moviesList[index]['description'],
             style: TextStyle(
               color: Colors.black,
               fontSize: 15,
             ),
           ),
           Row(
             children: [
               Text(moviesList[index]['rating'],
                 style: TextStyle(
                   color: Colors.black,
                   fontSize: 15,
                   fontWeight: FontWeight.bold,
                 ),
               ),
               Container(
                 margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                 child: Text(moviesList[index]['release_year'],
                   style: TextStyle(
                     color: Colors.black,
                     fontSize: 15,
                     fontWeight: FontWeight.bold,
                   ),
                 ),
               ),
             ],
           ),
           Divider(
             color: Colors.black,
           ),
         ],
       );
     }

     @override
     Widget build(BuildContext context) {
       return ListView.builder(
         padding: EdgeInsets.all(10),
         itemBuilder: _buildMovieItem,
         itemCount: moviesList.length,
       );
     }
    }
    ```

Obviously I have my own API information etc… in the main.dart file.

Hi Ross,

Do you need help with a Backendless specific query or a general Flutter question? We’d be happy to assist with the former, however, for questions outside of Backendless, it’d be better to ask on stackoverflow or any other Flutter-related forum.

Regards,
Mark

Hi @Ross_Coombe1

If you want to retrieve the data from the second table in your application, you can use the following code:

IDataStore<Map> yourTableNameStore = Backendless.data.of('YOUR_TABLE_NAME');
DataQueryBuilder queryBuilder = DataQueryBuilder();  // add necessary parameters to the query
yourTableNameStore.find(queryBuilder).then((response) {
    // handle server response (for example display it as list)
});

Best Regards,
Maksym