无法从 sqflite 数据库中获取所有用户并显示在带有 flutter 的列表中

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

我尝试从数据库中获取所有用户作为列表,并将它们显示到带有一些 DataColumn 和 DataCell 的 DataTable 中。但我从数据库中一无所获。列表是空的。

(我将数据存入数据库,因为我可以使用用户数据登录,并且在我的代码中有用于 show Text() 的部分,用于当我们没有数据时向用户显示“未找到数据”文本。

这是从数据库获取数据的代码:

 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;
  }

这是我的 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)
          ],
        ),
      ),
    );
  }
}

请指导我解决这个问题。谢谢

您可以尝试删除 getAllUsers() 中的 await 吗?

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