listflutterdatatabledatarowdatacolumn

can not get all users from sqflite database and show in list with flutter


I try to get all users from the database as a list and show them into DataTable with some DataColumn and DataCell. but I got just nothing from the database. and the list is empty.

(I have data into database because I can log in with user data and there is the section in my code for show Text() for when we have no data to show the user with No Data Found text.

this is code for getting data from the database:

 Future<List<User>> getUsers() async {
    var dbClient = await db;
    // List<Map> maps = await dbClient
    //     .query(USER_TABLE, columns: [ID, NAME, FAMILY, NATIONAL_ID]);
    List<Map> maps =
        await dbClient.query(USER_TABLE, columns: [ID, USERNAME, PASSWORD]);
    //List<Map> maps = await dbClient.rawQuery("SELECT * FROM $USER_TABLE");

    List<User> users = [];
    if (maps.length > 0) {
      for (int i = 0; i < maps.length; i++) {
        users.add(User.fromMap(maps[i]));
      }
    }
    return users;
  }

and this is my ShowUsersList class:

    import 'package:atlas_gen_demo/data/storage/db_helper.dart';
import 'package:flutter/material.dart';
import 'package:atlas_gen_demo/Animation/FadeAnimation.dart';
import '../models/user.dart';
import 'package:flushbar/flushbar.dart';

class UsersListScreen extends StatefulWidget {
  static const routeName = '/users-list-screen';

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

class _UsersListScreenState extends State<UsersListScreen> {
  Future<List<User>> usersList;
  var dbHelper;

  @override
  void initState() {
    super.initState();
    dbHelper = DBHelper();

    refreshList();
  }

  refreshList() {
    setState(() {
      usersList = dbHelper.getUsers();
    });
  }

  SingleChildScrollView dataTable(List<User> users) {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: DataTable(
        columns: [
          DataColumn(
            label: Text('id'),
          ),
          DataColumn(
            label: Text('name'),
          ),
          DataColumn(
            label: Text('family'),
          ),
          // DataColumn(
          //   label: Text('National Id'),
          // ),
        ],
        rows: users
            .map(
              (user) => DataRow(cells: [
                DataCell(Text(user.id.toString())),
                DataCell(Text(user.username)),
                DataCell(Text(user.password)),
                //DataCell(Text(user.nationalId)),
              ]),
            )
            .toList(),
      ),
    );
  }

  userList(BuildContext ctx) {
    return Expanded(
      child: FutureBuilder(
        future: usersList,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            showFlushBar(ctx, "test", snapshot.data);
            return dataTable(snapshot.data);
          }

          if (null == snapshot.data || snapshot.data.length == 0) {
            return Text(
              '"No Data Found',
            );
          }

          return CircularProgressIndicator();
        },
      ),
    );
  }

  void showFlushBar(BuildContext context, String title, String text) {
    Flushbar(
      padding: EdgeInsets.all(10),
      borderRadius: 8,
      backgroundGradient: LinearGradient(
        colors: [Colors.purple.shade800, Colors.purpleAccent.shade700],
        stops: [0.6, 1],
      ),
      boxShadows: [
        BoxShadow(
          color: Colors.black,
          offset: Offset(3, 3),
          blurRadius: 3,
        )
      ],
      dismissDirection: FlushbarDismissDirection.HORIZONTAL,
      forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
      titleText: Text(
        title,
        style: TextStyle(fontFamily: 'mainBold', color: Colors.white),
      ),
      messageText: Text(
        text,
        style: TextStyle(fontFamily: 'mainMedium', color: Colors.white),
      ),
      duration: Duration(seconds: 3),
    ).show(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              height: 250,
              margin: EdgeInsets.only(top: 50),
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/users_list.png'),
                  fit: BoxFit.fill,
                ),
              ),
            ),
            Positioned(
              child: FadeAnimation(
                  1.8,
                  InkWell(
                    child: Container(
                      margin: EdgeInsets.only(top: 10),
                      child: Center(
                        child: Text(
                          "Users List",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: Color.fromRGBO(143, 148, 251, 1),
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            fontFamily: 'persianBold',
                          ),
                        ),
                      ),
                    ),
                  )),
            ),
            userList(context)
          ],
        ),
      ),
    );
  }
}

may please guide me to fix this problem. thank you


Solution

  • Can you try and remove await in getAllUsers()?

    getAllUsers() async {
        users = dbHelper.getUsers();
      }