0
FlatButton(onPressed: () async {
        List<Map<String, dynamic>> queryRow = await 
DatabaseHelper.instance.queryAll(); print(queryRow);}, 
        child: Text('query'), color: Colors.green[400]),

above is the code, and I can print it to the console. How can I print the result of queryRow to new screen as a list? I would like to get your kind support.

Below is the data I printed to the console:

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

 

FavoritesPage

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"]}"),
          );
        },
      ),
    );
  }
}
dm_tr
  • 4,265
  • 1
  • 6
  • 30

1 Answers1

1

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

dm_tr
  • 4,265
  • 1
  • 6
  • 30
  • Thank you so much for your response. I tried but received below comment. The getter 'length' was called on null. Receiver: null Tried calling: length – cyborg2006 Dec 26 '20 at 16:18
  • You need to put your `queryRow` variable in a global scope – dm_tr Dec 26 '20 at 16:21
  • I am new in Flutter, it is shame, but I couldn't do it. I am going to check from net how to do it. – cyborg2006 Dec 26 '20 at 16:30
  • Thank you. Flat Button has an error Error: 'await' can only be used in 'async' or 'async*' methods. queryRow = await DatabaseHelper.instance.queryAll(); – cyborg2006 Dec 26 '20 at 16:46
  • Oh, sorry for typo. Just change `async` place. Try the last code – dm_tr Dec 26 '20 at 16:50
  • The following assertion was thrown while handling a gesture: setState() callback argument returned a Future. The setState() method on _WelcomePageState#eed6e was called with a closure or method that returned a Future. Maybe it is marked as "async". Instead of performing asynchronous work inside a call to setState(), first execute the work (without updating the widget state), and then synchronously update the state inside a call to setState(). – cyborg2006 Dec 26 '20 at 17:17
  • Hope the this latest one will work for you – dm_tr Dec 26 '20 at 17:19
  • Still change you FlatButton section – dm_tr Dec 26 '20 at 17:20
  • When I pressed the FlatButton message (flutter: Reading data form phone cache...). On the other hand when I go to new page for ListView.builder error (The following NoSuchMethodError was thrown building FavoritesPage(dirty): The getter 'length' was called on null. Receiver: null Tried calling: length ). Problem is here: itemCount: queryRow.length, – cyborg2006 Dec 26 '20 at 17:27
  • My example supposes that the variable, the FlatButton and the ListView.builder are on the same page – dm_tr Dec 26 '20 at 17:28
  • If you need further assist, please post the full code of your page – dm_tr Dec 26 '20 at 17:29
  • Thanks you for your support. I added FavoritesPage.dart file to show you. I am tryin to show the list of query to that page, but length is coming null. – cyborg2006 Dec 26 '20 at 17:36
  • Also post the code of your page that contains your FlatButton – dm_tr Dec 26 '20 at 17:43
  • I am trying but stack overflow is not accepting even I put it in (Crtl+K) – cyborg2006 Dec 26 '20 at 18:05