我有一个应用程序,我建立了一个帖子页面,一切都很好,在应用程序中,当我打开,去添加一个帖子页面,并写了也没关系,这个错误发生时,我点击了帖子按钮。我使用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),
),
],
),
),
);
}
}
我是新来的,请帮帮忙。?
1条答案
按热度按时间vwkv1x7d1#
当您尝试对
null
值使用!
运算符时,会发生此异常。就你而言,如果
user
的值不为null,可能当您初始化提供程序currentUserDetailsProvider
时,它会使用null
的内在值进行构建。因此,您可以接受user
可以是null
,使用?
操作符,或者修改提供程序,使构建时的值不是null
。