0

I don't understand futures well in flutter and its been a persistent struggle through all my apps. This is one of those errors. When I remove the "[]" it shows "instance of 'Future'" and that's about it.

Class 'Future<dynamic>' has no instance method '[]'.
Receiver: Instance of 'Future<dynamic>'
Tried calling: [](0)
class ListOfEntries extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       shape: RoundedRectangleBorder(borderRadius: BorderRadius.vertical(bottom: Radius.circular(30))),
       title: Text("Your entries"),
       toolbarHeight: 50,
     ),
     body: FutureBuilder<List>(
       future: Database_().entries(),
       builder: (context, snapshot) {
         return snapshot.hasData ?
         new ListView.builder(
           padding: const EdgeInsets.all(10.0),
           itemCount: snapshot.data.length,
           itemBuilder: (context, i) {
             return Column(
               children: [
                 Text("${GetList()[i]}"),
               ],
             );
           },
         )
             : Center(
           child: Column(
           crossAxisAlignment: CrossAxisAlignment.center,
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               CircularProgressIndicator(),
               Text(""),
               Text("No entries yet:("),
             ],             
           
           )
         );
       }
    ),

    );

  }
  GetList()async{
    final list = await Database_().entries();
    return list;
  }

}

This is the function that returns the list.

Future<List<Entry_>> entries() async {
    final Database db = await database;
    final List<Map<String, dynamic>> maps = await db.query('[Journal Entries]');
    return List.generate(maps.length, (i) {
      return Entry_(
        maps[i]["_entry"],
        maps[i]["_date"],
      );
    });
  }
Nush
  • 55
  • 1
  • 7
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Apr 20 '21 at 06:52

2 Answers2

0

Usually, when you use a FutureBuilder you pass the future you want to complete and then use the data from that future for your widgets. I believe you should call the function GetList() instead of Database_().entries() and output on the text the snapshot.data[i].entrywhatever

user14624595
  • 874
  • 7
  • 19
0

You can get rid of the GetList funtion since you are using FutureBuilder. Try the following:

  1. Delete the GetList function.
  2. change future: Database_().entries() to future: Database_().entries
  3. change Text("${GetList()[i]}") to Text(${snapshot.data[i]})
ambiguous58
  • 1,241
  • 1
  • 9
  • 18
  • Okay so I tried that and changing future: Database_().entries() to future: Database_().entries gives this error :The argument type 'Future> Function()' can't be assigned to the parameter type 'Future>'. – Nush Apr 19 '21 at 17:17
  • Also putting the snapshot in the text gives out 'instance of "Entry_"' – Nush Apr 19 '21 at 17:18
  • Can you set a breakpoint at that line and see what is "inside" of snapshot.data? That will help us find the solution. – ambiguous58 Apr 20 '21 at 02:36