为什么我必须热重载 flutter 才能让未来的构建器返回

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

我有一个问题,当我从手机启动作业页面时,未来不会返回任何列表,但当我热重新加载时,会返回一个列表。

这是从 Sqflite 数据库获取列表的 Future 代码。

 Future\<List\<Road\>\> fetchARoad(int ClientID) async {
final db = \_db;
if (db == null) {
return \[\];
}
var read = await db.query('ROAD',
distinct: true,
columns: \[
'RoadID',
'RoadName',
'RoadNo',
'FName',
'LName',
'EId',
'ESubId',
'StationName',
'OicName',
'Lane',
'CarriageWay',
'ClientID'
\],
orderBy: 'RoadID',
where: "ClientID = ?",
whereArgs: \[ClientID\]);
// final road = read.map((row) =\> Road.fromRow(row)).toList();
List\<Road\> roadlist =
read.isNotEmpty ? read.map((e) =\> Road.fromRow(e)).toList() : \[\];
return roadlist;

}

这是未来的构建器代码:

 FutureBuilder<List<Road>>(
            future: _roadStorage.fetchARoad(widget.currentClientId.toInt()),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return CircularProgressIndicator();
              } else if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              } else if (!snapshot.hasData) {
                return Text('No data available.');
              } else { 
              final roads = snapshot.data as List<Road>;
              print(roads);
              return 
                   Expanded(
                      child: ListView.builder(
                        itemCount: roads.length,
                        itemBuilder: (context, index) {
                          final road = roads[index];
                          return Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: ListTile(
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10)),
                              tileColor: Colors.white60,
                              onTap: () {
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) =>
                                            const VehiclePage()));
                              },
                              leading: Text(
                                'ID:${road.ClientID}',
                                style: const TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                              title: Text(
                                road.roadName,
                                style: const TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 18,
                                ),
                              ),
                              subtitle: Text(
                                roadi.eFname,
                                style: const TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 18,
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                    );
                  
            }
            },
          ),

我尝试将 Future 更改为 Stream,但仍然遇到同样的问题。 我只是不知道问题出在哪里,因为我没有收到任何错误。

flutter future sqflite flutter-futurebuilder
1个回答
0
投票

因此,我没有找到解决必须重新加载以通过“ClientID”从数据库获取数据的问题的解决方案,因此我决定获取所有记录,然后使用“Stream”“where”方法过滤记录。

像这样:

 Stream<List<Road>> roadsById(int ClientID) {
return _streamController.stream.map((roads) {
  return roads.where((road) => road.ClientID == ClientID).toList();
});

希望它可以帮助人们摆脱头痛。

© www.soinside.com 2019 - 2024. All rights reserved.