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

1 回答 1

0

Edit
Proceed as following:

Your queryRow variable declaration and initialization

List<Map<String, dynamic>> queryRow;

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

  queryRow = []; // initialization
}

Your FlatButton()

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

Your ListView.builder() widget

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

Feel free to ask in comment section if you encounter any issue

于 2020-12-26T14:52:35.467 回答