0

So i have a problem with the futurebuilder. first here is my code:

FutureBuilder(
              future: DBProvider.db.getAllTasks(),
              builder:(BuildContext context, AsyncSnapshot snapshot) {
                              if (snapshot.connectionState ==
                                  ConnectionState.waiting) {
                                return Center(
                                  child: Text("Loading..."),
                                );
                              } else if (snapshot.hasData) {
                                print(snapshot.data);
                                return Column(
                                  children: List.generate(snapshot.data.length,
                                      (index) {
                                    return Text(snapshot.data[index].title); // the problem is here
                                  }),
                                );
                                     } else {
                                             return Center(
                                             child: CircularProgressIndicator());
                                     }
}),

I fetch data with DBProvider.db.getAllTasks() that will return a list, then i use the builder to render this data, the snapshot.data is not equal to null because when i printed the snapshot.data it shows that there is an instance of each item in the list. The problem is when i try to display the list, it gives me this error: A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 360 pos 10: 'data != null'

and this error is because of the text widget. How can i resolve this problem ? Thanks in advance.

  • is the title null? because you can't pass a null string to a text widget, so it is not necessarily the snapshot.data that is null – Nayef Radwi Nov 13 '20 at 23:05
  • Thanks, yeh it seems like this is the problem i will review my code of sql and see if it's inserting items in the database. thanks for the help – YOUCEF ABDELLICHE Nov 13 '20 at 23:52

2 Answers2

0

Try doing this:

FutureBuilder(
    future: DBProvider.db.getAllTasks(),
    builder:(BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.connectionState ==
          ConnectionState.waiting) {
        return Center(
          child: Text("Loading..."),
        );
      } else if (snapshot.hasData && snapshot.connectionState == ConnectionState.done) { /// to ensure that data is not "null"
        print(snapshot.data);
        return Column(
          children: List.generate(snapshot.data.length,
              (index) {
            return Text(snapshot.data[index].title); 
          }),
        );
      } else {
          return Center(
          child: CircularProgressIndicator());
      }
}),

Let me know if it works...

Chichebe
  • 580
  • 5
  • 12
0

Did you debug check, if each title has a value and is not null? Try debugging with the following code :

.....
print(snapshot.data);
List.generate(snapshot.data.length,
       (index) {                      
          print('$index 
          ${snapshot.data[index].title}');
     }),
.....

If any of titles has a null you will know the reason for error.

bluenile
  • 5,673
  • 3
  • 16
  • 29
  • Thanks, yeh it seems like this is the problem i will review my code of sql and see if it's inserting items in the database. thanks for the help – YOUCEF ABDELLICHE Nov 13 '20 at 23:52