无法从sqflite数据库中获取所有用户,并在列表中颤动显示

问题描述 投票:0回答:1

我尝试以列表的形式从数据库中获取所有用户,并通过一些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类:

    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)
          ],
        ),
      ),
    );
  }
}

请指导我解决此问题。谢谢

list flutter datatable datarow datacolumn
1个回答
0
投票

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

getAllUsers() async {
    users = dbHelper.getUsers();
  }
© www.soinside.com 2019 - 2024. All rights reserved.