我有一个应用程序,我构建了一个帖子页面,当我打开并去添加帖子页面并写入时,一切都运行良好,也可以,当我单击帖子按钮时发生此错误。我使用 Appwrite 来保存数据,并且帖子未保存在 appwrite 数据库中
所以当我单击按钮时出现此错误,他将我带到 post_controller.dart 错误:
Exception has occurred.
_TypeError (Null check operator used on a null value)
错误所在的代码:
void _shareTextPost({
required List<File> images,
required String text,
required BuildContext context,
}) async {
state = true;
final hashtags = _getHashtagsFromText(text);
String link = _getLinkFromText(text);
final user = _ref.read(currentUserDetailsProvider).value!;
//
Post post = Post(
text: text,
hashtags: hashtags,
link: link,
imageLinks: [],
uid: user.uid,
postType: PostType.text,
postedAt: DateTime.now(),
likes: [],
commentIds: [],
id: '',
reshareCount: 0,
repostedBy: '',
repliedTo: '',
);
final res = await _postAPI.sharePost(post);
state = false;
res.fold((l) => showSnackBar(context, l.message), (r) => null);
}
帖子页面中的代码:
class CreatePostScreen extends ConsumerStatefulWidget {
static route() => MaterialPageRoute(
builder: (context) => const CreatePostScreen(),
);
const CreatePostScreen({super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_CreatePostScreenState();
}
class _CreatePostScreenState extends ConsumerState<CreatePostScreen> {
final postTextController = TextEditingController();
List<File> images = [];
@override
void dispose() {
super.dispose();
postTextController.dispose();
}
void sharePost() {
ref.read(postControllerProvider.notifier).sharePost(
images: images,
text: postTextController.text,
context: context,
);
}
void onPickImages() async {
images = await pickImages();
setState(() {});
}
@override
Widget build(BuildContext context) {
final isLoading = ref.watch(postControllerProvider);
//final currentUser = ref.watch(currentUserDetailsProvider).value;
//
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.close,
size: 30,
color: Colors.black,
),
),
actions: [
Padding(
padding: const EdgeInsets.only(
right: 16.0), // Adjust the padding as needed
child: RoundedSmallButton(
onTap: sharePost,
label: 'Post',
backgroundColor: Pallete.pinkColor,
TextColor: Pallete.whiteColor,
),
),
],
),
// ignore: unnecessary_null_comparison
body: isLoading == null
//|| currentUser == null
? const Loader()
: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
Row(
children: [
// Padding(
// padding: const EdgeInsets.all(
// 10.0), // Adjust the padding as needed
// child: CircleAvatar(
// backgroundImage: NetworkImage(currentUser.profilePic),
// radius: 30,
// ),
// ),
const SizedBox(width: 2),
Expanded(
child: TextField(
controller: postTextController,
style: const TextStyle(
fontSize: 18,
color: Colors.black,
// fontWeight: FontWeight.bold,
),
decoration: const InputDecoration(
hintText: "Got study tips to share?",
hintStyle: TextStyle(
color: Pallete.greyColor,
fontSize: 18,
fontWeight: FontWeight.w600,
),
border: InputBorder.none,
// focusedBorder: UnderlineInputBorder(
// borderSide: BorderSide(color: Pallete.pinkColor),
// ),
),
maxLines: null,
cursorColor: Pallete.pinkColor,
)),
],
),
if (images.isNotEmpty)
CarouselSlider(
items: images.map(
(file) {
return Container(
width: MediaQuery.of(context).size.width,
margin: const EdgeInsets.symmetric(
horizontal: 5,
),
child: Image.file(file));
},
).toList(),
options: CarouselOptions(
height: 400,
enableInfiniteScroll: false,
),
),
],
)),
),
bottomNavigationBar: Container(
padding: const EdgeInsets.only(bottom: 10),
decoration: const BoxDecoration(
border: Border(
top: BorderSide(
color: Pallete.greyColor,
width: 0.3,
)),
),
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(10.0).copyWith(
left: 15,
right: 15,
),
child: GestureDetector(
onTap: onPickImages,
child: SvgPicture.asset(AssetsConstants.galleryIcon)),
),
Padding(
padding: const EdgeInsets.all(10.0).copyWith(
left: 15,
right: 15,
),
child: SvgPicture.asset(AssetsConstants.gifIcon),
),
Padding(
padding: const EdgeInsets.all(10.0).copyWith(
left: 15,
right: 15,
),
child: SvgPicture.asset(AssetsConstants.emojiIcon),
),
],
),
),
);
}
}
我是颤振新手,请帮忙??
当您尝试对
!
值使用 null
运算符时,会发生此异常。在你的情况下,在行中
final user = _ref.read(currentUserDetailsProvider).value!;
您尝试检查
user
的值是否不为空。可能当您初始化提供者 currentUserDetailsProvider
时,它会使用 null
的内在值进行构建。因此,您要么接受 user
可以是 null
,而是使用 ?
运算符,要么修复提供程序,以便构建时的值不是 null
。