0

i work with an exist sqlite database, and i want to display a random row from it. i followed this steps , but it display the row with the name of the column and the brakets.

this is the code i used :

  var name;
  void _query() async {
    Database db = await DatabaseHelper.instance.database;
    List<Map> result = await db.rawQuery('SELECT content FROM $table ORDER BY random() LIMIT 1');
    setState(() {
      result.forEach((row){
        print(row);
      name= result;
    });
    });
  }

in the builder, i use this :

 Container(
          child: Text('${name}'),
          ),

RaisedButton(
              child: Text('Generate another'),
              onPressed: (){
                _query()
                ;},
            ),

the result in terminal {content:james}

and in the emulator [{content:james}]

i want to display just : james

thank you

swing13
  • 33
  • 1
  • 12

2 Answers2

1

result is a List of Map so you can use first & correct key to access the desired result.

setState(() {
    name = result.first['content']; // james
});
Tirth Patel
  • 5,443
  • 3
  • 27
  • 39
  • I'm glad I could help you. – Tirth Patel Oct 08 '20 at 10:24
  • i add a search fonction to the same database, and i got the same result, i use the same solution but it didn't work, can you please help me, thank you https://stackoverflow.com/q/64404274/12832387 – swing13 Oct 17 '20 at 15:45
0

A solution would be to convert your output to a list before going through it.

outputList = result.values.toList(); as it is stated in this answer

MihaiBC
  • 472
  • 5
  • 15