我尝试将数据库中的所有用户作为列表获取,并使用一些DataColumn和DataCell将它们显示到DataTable中。 但我从数据库中什么也没得到。 并且列表为空。
(我有数据进入数据库,因为我可以使用用户数据登录,并且我的代码中有用于显示 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)
],
),
),
);
}
}
可以指导我解决这个问题。
谢谢你能尝试在getAllUsers((中删除await吗?
getAllUsers() async {
users = dbHelper.getUsers();
}