我有一個頁面來創建帖子。我正在使用 image_picker 包來獲取影像,并且有一個提供者可以將影像獲取到帖子創建頁面。有一個變數selectedImagesList存盤用戶選擇后要顯示的選擇影像串列。
class _AddPostViewState extends State<AddPostView> {
TextEditingController postTextController = TextEditingController();
PostNotifier postNotifier(bool renderUi) =>
Provider.of<PostNotifier>(context, listen: renderUi);
int activeImageIndex = 0;
List<File>? selectedImagesList = [];
@override
void initState() {
postTextController = TextEditingController();
super.initState();
}
@override
void dispose() {
print("disposing stuff");
selectedImagesList = [];
super.dispose();
}
@override
Widget build(BuildContext context) {
selectedImagesList = postNotifier(true).selectedPostImages;
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: const Text(
"New Post",
style: TextStyle(color: Colors.black),
),
centerTitle: true,
),
body: SingleChildScrollView(
child: Column(children: [
Column(
Container(
child: TextField(
controller: postTextController,
decoration: const InputDecoration(
hintText: "Write something here..."),
),
),
const SizedBox(
height: 5,
),
ElevatedButton(
onPressed: () {
postNotifier(false).pickPostImages();
},
child: selectedImagesList == null
? const Text("Add Images")
: const Text("Reselect Images")),
if (postNotifier(true).selectedPostImages != null) ...[
Container(
padding: const EdgeInsets.all(25.0),
height: 420,
width: 396,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CarouselSlider.builder(
options: CarouselOptions(
onPageChanged: (index, reason) =>
setState(() => activeImageIndex = index),
),
itemCount: selectedImagesList!.length,
itemBuilder:
(BuildContext context, index, realIndex) {
final selectedImage = selectedImagesList?[index];
return buildImage(selectedImage!, index);
}),
const SizedBox(
height: 21,
),
buildDotIndicator(),
],
),
),
] else ...[
const SizedBox(
height: 0,
),
],
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
final authenticationNotifier =
Provider.of<AuthenticationNotifier>(context,
listen: false);
var userEmail = await authenticationNotifier
.fetchUserEmail(context: context);
var postText = postTextController.text;
await postNotifier(false)
.uploadPostImageList(context: context);
var postMedia = postNotifier(false).uploadedImageUrlList;
if (postMedia != null) {
await postNotifier(false)
.addMultiImagePost(
context: context,
//post dto
)
.whenComplete(
() {
SnackBarUtility.showSnackBar(
message: "Post added to database",
context: context);
postTextController.clear();
selectedImagesList = [];
Navigator.of(context).popAndPushNamed(HomeRoute);
},
);
} else {
SnackBarUtility.showSnackBar(
message: "Something went wrong", context: context);
}
},
child: const Text("Post")),
],
)
]),
),
),
);
我可以postTextController通過使用清除,.whenComplete()但selectedImagesList仍然存在,后續帖子有來自以前帖子的影像。
從上面的代碼中可以明顯看出,我也嘗試selectedImagesList在函式中設定為空,void dispose()但它不起作用。上傳帖子后如何處理影像
uj5u.com熱心網友回復:
您沒有selectedImagesList在狀態中使用定義,而是selectedImagesList在PostNotifier類中使用定義。所以在 dispose 方法中你應該清楚selectedImagesList地在PostNotifier類中定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/430235.html
上一篇:如何使用PIL.ImageDraw.Draw.text設定錨點以適合影像中心的文本?
下一篇:將資料從修改器傳遞給畫家
