错误是:
The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<List<Map<dynamic, dynamic>>>', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.
我试着添加了一个其他函数,但是我不知道该在哪里添加它。更别提了,我不知道这是否是解决这个错误的最佳方法。
这是我的密码:
import 'package:algorithm_learn/sqldb.dart';
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
SqlDb sqlDb = SqlDb();
bool isloading = true;
List coing = [];
Future<List<Map>> readData() async {
List<Map> response = await sqlDb.readData("SELECT * FROM 'Coing'"); //(ERROR IS HERE)
coing.addAll(response);
isloading = false;
if (this.mounted) {
setState(() {});
}
}
@override
void initState() {
readData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('HomePage'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).pushNamed("addperson");
},
child: const Icon(Icons.add),
),
body: isloading == true ?
Center(child: Text("Loading..."))
: Container(
child: ListView(
children: [
MaterialButton(
onPressed: () async {
await sqlDb.mydeleteDatabase();
},
child: const Text("Delete Database"),
),
ListView.builder(
itemCount: coing.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, i) {
return Card(
child: ListTile(
title: Text("${coing[i]['first_name']}"),
subtitle: Text("${coing[i]['last_name']}"),
trailing: IconButton(
onPressed: () async {
int response = await sqlDb.deleteData(
"DELETE FROM coing WHERE id = ${coing[i]['id']}");
if (response > 0) {
coing.removeWhere(
(element) => element["id"] == coing[i]['id']);
setState(() {});
}
},
icon: Icon(Icons.delete, color: Colors.red),
),
),
);
},
),
],
),
),
);
}
}
发布于 2022-08-18 18:19:28
可以将其标记为可空,方法是添加?
Future<List<Map>> readData() async {
List<Map>? response = await sqlDb.readData("SELECT * FROM 'Coing'");
if(response != null)
{
coing.addAll(response);
}
isloading = false;
if (this.mounted) {
setState(() {});
}
}
https://stackoverflow.com/questions/73407060
复制相似问题