只有导航器在Inkwell的onTap()中工作



我正在列出通知。我想在用户点击通知时更新数据库中的读取状态。我在Inkwell小部件的OnTap((函数中使用了update函数,并导航到下一页。但是异步函数没有调用。有什么可能的办法解决这个问题吗?

这是我的代码:

class NotificationList extends StatefulWidget {
const NotificationList({Key? key}) : super(key: key);
@override
_NotificationListState createState() => _NotificationListState();
}
class _NotificationListState extends State<NotificationList> {
String useremail = '';
String appusertype = '';
Future getList() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
useremail = preferences.getString('useremail') ?? "Not found";
//debugPrint(useremail);
// debugPrint(appusertype);
});
Map<String, String> headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Charset': 'utf-8'
};
Map<String, dynamic> body = {
"email": useremail,
};
var bodyEncoded = body.keys.map((key) => "$key=${body[key]}").join("&");
//print(bodyEncoded);
var url = "/notification_list.php";
var response = await http.post(Uri.parse(ApiConstant.baseUrl + url),
headers: headers,
encoding: Encoding.getByName('utf-8'),
body: bodyEncoded);

return json.decode(response.body);
}
Future updateStatus(id) async {
Map<String, String> headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Charset': 'utf-8'
};
Map<String, dynamic> body = {
"id": id,
};
var bodyEncoded = body.keys.map((key) => "$key=${body[key]}").join("&");
//print(bodyEncoded);
var url = "/update_notification.php";
var response = await http.post(Uri.parse(ApiConstant.baseUrl + url),
headers: headers,
encoding: Encoding.getByName('utf-8'),
body: bodyEncoded);
print(id);
print("object");
return jsonDecode(response.body);
}
@override
void initState() {
super.initState();
// checkLogin();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 30, 64, 122),
title: const Text(
'Notifications',
style: TextStyle(color: Colors.orange),
),
centerTitle: true,
bottom: const PreferredSize(
preferredSize: Size.zero,
child: Text(
"عرض قائمة الممتلكات",
style: TextStyle(color: Colors.orange),
)),
),
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/building.jpg"),
fit: BoxFit.cover,
)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: FutureBuilder(
future: getList(),
builder: ((context, AsyncSnapshot snapshot) {
if (snapshot.hasError) print(snapshot.error);
//print("object");
if (snapshot.hasData) {
List list = snapshot.data as List;
if (list.isEmpty) {
return const Center(
child: Card(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(20.0),
child: Text('No Notifications yet!'),
)));
}
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: ((context, index) {
List? list = snapshot.data as List?;
return InkWell(
onTap: () async {
updateStatus(list![index]['id']);
print(list[index]['id']);
list[index]['title'] == "Interest Request"
? Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const InterestRequestList()))
: null;
},
child: Card(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Row(
children: [
// Container(
//   width: 50,
//   height: 70,
// decoration: const BoxDecoration(
//   image: DecorationImage(
//               image: AssetImage ("assets/building.jpg"),
//               fit:BoxFit.cover,
//               )
// ),),
const Expanded(
flex: 1,
child: Icon(
Icons.notifications,
color: Colors.grey,
),
),
//Spacer(),
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.fromLTRB(
10, 0, 0, 0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
list![index]['read_status'] == '0'
? Text(
list[index]['title'],
style: const TextStyle(
color: Colors.orange,
fontSize: 15),
)
: Text(
list[index]['title'],
style: const TextStyle(
color: Colors.orange,
fontSize: 15),
),
Text(list[index]['body']),
]),
),
),
//const Spacer(),
const Expanded(
flex: 1,
child: Icon(Icons.arrow_forward_ios)),
],
),
),
),
);
}));
}
return const Center(
child: Card(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(20.0),
child: Text('No Notifications yet!'),
)));
// else {
//   return const Center(
//     child: CircularProgressIndicator(),
//   );
// }
}),
)),
));
}
}

updateStatus调用前面缺少一个await

await updateStatus(list![index]['id']);

最新更新