如何正确地将所有数据从Sqlite数据库显示到ListView.builder中?

时间:2020-11-09 22:59:08

标签: flutter flutter-dependencies

我正在尝试访问我一直保存到sqlite数据库中的所有数据。数据库中充满了Quarter对象。我想使用ListView.Builder将它们全部显示。只是发出我对我的情况不完全正确的了解。我目前遇到的错误为type 'QueryRow' is not a subtype of 'List<dynamic>' ,我将标记IDE指出此错误的来源。向我展示我在做什么和纠正我有任何帮助,我们将不胜感激!谢谢!

这是我目前拥有的。我已经标出了我上面详述的错误应该来自哪里

class ChangeQuarter extends StatefulWidget {
  @override
  _ChangeQuarterState createState() => _ChangeQuarterState();
}

class _ChangeQuarterState extends State<ChangeQuarter> {
  Future<void> _future;
  static const Color greycolor = Color.fromRGBO(220, 220, 220, 10);

  @override
  initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(95.0),
          child: AppBar(
            automaticallyImplyLeading: false, // hides leading widget
            flexibleSpace: QuarterAppBar(),
          ),
        ),
        body: FutureBuilder<dynamic>(
            future: QuarterDBProvider.quarterDB.getQuarter(),
            initialData: List(),
            builder: (context, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text('none');
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                case ConnectionState.active:
                  return Text('');
                case ConnectionState.done:
                  if (snapshot.hasError) {
                    print(
                      '${snapshot.error}',
                    );
                  }
              }
=> ERROR LOCATION   List quarter = snapshot.data;
                    return ListView.builder(
                      itemCount: snapshot.data.length,
                      shrinkWrap: true,
                      itemBuilder: (context, int index) {
                        final quarter = snapshot.data[index];
                        return Padding(
                          padding: EdgeInsets.symmetric(
                              vertical: 1.0, horizontal: 4.0),
                          child: Card(
                            color: (index % 2 == 0) ? greycolor : Colors.white,
                            child: Container(
                              height: 60,
                              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
                              child: InkWell(
                                onTap: () => Navigator.pop(context),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Container(
                                      margin: EdgeInsets.only(right: 5),
                                      child: Text(snapshot.data[index].quarter,
                                          style: TextStyle(
                                              fontSize: 20,
                                              fontFamily: 'Montserrat',
                                              color: Colors.blue),
                                          textAlign: TextAlign.left),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        );
                      },
                    );

            }));
  }
}

1 个答案:

答案 0 :(得分:0)

该错误基本上是说您正在尝试使用List<dynamic>的地方使用QueryRowQuarterDBProvider.quarterDB.getQuarter()返回一个QueryRow,您在声明“未来”构建器,如下所示:FutureBuilder<List<dynamic>>,而它应该是FutureBuilder<QueryRow>。另外,您应将List quarter = snapshot.data;改成QueryRow quarter = snapshot.data;

如果您需要其他帮助,请告诉我如何处理QueryRow,但我想您已经知道了,因为您正在使用SQlite