【问题标题】:How can I show SQLite mapped data to new flutter screen. I can print it to the console but not in flutter screen如何将 SQLite 映射数据显示到新的颤动屏幕。我可以将它打印到控制台,但不能在颤振屏幕中
【发布时间】:2021-04-04 00:55:45
【问题描述】:
FlatButton(onPressed: () async {
        List<Map<String, dynamic>> queryRow = await 
DatabaseHelper.instance.queryAll(); print(queryRow);}, 
        child: Text('query'), color: Colors.green[400]),

上面是代码,我可以打印到控制台。如何将 queryRow 的结果作为列表打印到新屏幕?我希望得到您的支持。

下面是我打印到控制台的数据:

        [{_id: 1, name: John}, {_id: 2, name: Hans}, {_id: 3, name: Carol}]

 

收藏页面

import 'package:flutter/material.dart';

class FavoritesPage extends StatelessWidget {
  static String id = 'favoritesPage';

  @override
  Widget build(BuildContext context) {
    var queryRow;
    return Scaffold(

      appBar: AppBar(
        backgroundColor: Colors.transparent,
        shadowColor: Colors.transparent,
        iconTheme: IconThemeData(color: Colors.orange),
        centerTitle: true,
        title: const Text('Favorites', style: TextStyle(color: Colors.orange)),
        elevation: 0,
      ),
      body: ListView.builder(
        itemCount: queryRow.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("${queryRow[index]["name"]} - ID: ${queryRow[index]["id"]}"),
          );
        },
      ),
    );
  }
}

【问题讨论】:

    标签: sqlite flutter flutter-futurebuilder


    【解决方案1】:

    编辑
    进行如下操作:

    您的 queryRow 变量声明和初始化

    List<Map<String, dynamic>> queryRow;
    
    @override
    void initState() {
      super.initState();
    
      queryRow = []; // initialization
    }
    

    你的 FlatButton()

    FlatButton(
      onPressed: () async {
        final data = await DatabaseHelper.instance.queryAll();
        setState(() {
           queryRow = data;
        });
      },
      child: Text('Query'),
    ),
    

    您的 ListView.builder() 小部件

    ListView.builder(
      itemCount: queryRow.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text("${queryRow[index]["name"]} - ID: ${queryRow[index]["id"]}"),
        );
      },
    ),
    

    如果您遇到任何问题,请随时在评论部分询问

    【讨论】:

    • 非常感谢您的回复。我试过但收到以下评论。在 null 上调用了 getter 'length'。接收者:null 尝试调用:长度
    • 您需要将queryRow 变量放在全局范围内
    • 我是 Flutter 的新手,很遗憾,但我做不到。我要从网上检查如何做到这一点。
    • 谢谢。 Flat Button 有一个错误错误:“await”只能用于“async”或“async*”方法。 queryRow = 等待 DatabaseHelper.instance.queryAll();
    • 哦,抱歉打错了。只需更改async 的位置。试试最后的代码
    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 2019-08-13
    • 2021-01-24
    • 1970-01-01
    • 2021-11-29
    • 2021-07-12
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多