I want to do a to do list app. I use Sqflite in my project.
Widget
`
return FutureBuilder(
future: databaseTrans.getTables(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return taskBookErrorWidget(context);
} else {
return GridView.count(
crossAxisCount: 3,
children: List.generate(snapshot.data!.length, (i) {
return InkWell(
onTap: () {},
child: Container(
width: 300,
height: 300,
margin: EdgeInsets.all(10),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white10,
borderRadius: BorderRadius.circular(30),
),
child: Text(
snapshot.data![i].name.toString(),
style: TextStyle(color: Colors.white)),
),
);
}),
);
}
},
);
}
`
Errors
`
The following _CastError was thrown building FutureBuilder<List<taskBooksDataModel>>(state: _FutureBuilderState<List<taskBooksDataModel>>#da7bd):
Null check operator used on a null value
The relevant error-causing widget was:
FutureBuilder<List<taskBooksDataModel>> FutureBuilder:file:///C:/Users/Administrator/Desktop/todolist/lib/screens/taskBooksScreen/taskBookWidget.dart:16:12
When the exception was thrown, this was the stack:
#0 _SnackBarState.initState (package:flutter/src/material/snack_bar.dart:391:21)
#1 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5015:57)
#2 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
#3 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#4 Element.updateChild (package:flutter/src/widgets/framework.dart:3586:20)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4904:16)
`
Database Function
`
Future<List<taskBooksDataModel>> getTables() async {
Database db = await database;
var result = await db.query("sqlite_master");
return taskBooksDataModel.convertToTaskBookDataModel(result);
}
`
What should i do ? I want to read datas from table and write them into Text() on FutureBuilder. I have two tables. One of them includes task books' names.
CodePudding user response:
try checking ConnectionState first. then you should check data of snapshot.
if (snapshot.connectionState == ConnectionState.waiting){
return CircularProgressIndicator();
}
CodePudding user response:
That's basically means that you've used '!' in a place where variable is null. Check to see if you returned null from your API. Also take a look at docs to see how to check if snapshot has data
CodePudding user response:
Code in your sample says that data
in snapshot.data![i].name.toString()
is never null
but during execution it turns out sometimes it is null
. Add some condition for proper handling input data in Text Widget. E.G.
Text(
snapshot.data != null
? snapshot.data![i].name.toString()
: '',
style: TextStyle(color: Colors.white),
),
),