I have an app I built a post page everything worked good in app when I opened and go to add a post page and wrote is also okay this error happened when I clicked on the post button.. I use Appwrite to save data and the posts do not saved in appwrite database
so this error when I click on button he take me to post_controller.dart the error :
Exception has occurred.
_TypeError (Null check operator used on a null value)
the code where the error is :
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);
}
the code in the post page :
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),
),
],
),
),
);
}
}
I am new at flutter please help ??
This exception occurs when you try to use the
!
operator on a value that isnull
. In your case, in the lineyou try to check if the value of
user
is not null. Probably when you initialize the providercurrentUserDetailsProvider
it builds with an intrinsic value ofnull
. So you either accept that theuser
can benull
, using the?
operator instead, or fix the provider so that the value on build is notnull
.