[ad_1]
at any time when I run my program, an exeption happens the place res comes again as null. This system profitable uploads to firebase, and the enter worth is not empty, so I am undecided why the worth does not come again as something.
Right here is the code concerned.
The road occus on this line of the _onPublish Perform, “builder: (context) => NewsPostPage(newsPostId: res!)));”, and it reads, “_CastError (Null verify operator used on a null worth)”
When a button is pressed, it calls operate _onPublish
_onPublish() async {
if (_formKey.currentState!.validate()) {
setState(() {
_isLoading = true;
});
await DatabaseService()
.saveNewsPost(
_titleEditingController.textual content, _contentEditingController.textual content)
.then((res) async {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => NewsPostPage(newsPostId: res!)));
_isLoading = true;
print("Submit Uploaded to Firebase");
});
}
}
Right here is operate Database Service.saveNewsPost()
Future saveNewsPost(
String title,
String content material,
) async {
DocumentReference newsPostsRef =
await FirebaseFirestore.occasion.assortment('newsPosts').add({
'newsPostId': '',
'newsPostTitle': title,
'newsPostTitleArray': title.toLowerCase().break up(" "),
//'newsPostAuthor': creator,
'newsPostContent': content material,
'createdAt': new DateTime.now(),
'date': DateFormat.yMMMd('en_US').format(DateTime.now())
});
await newsPostsRef.replace({'newsPostId': newsPostsRef.id});
//return
newsPostsRef.id;
}
Lastly, right here is NewsPostPage
class NewsPostPage extends StatefulWidget {
last String newsPostId;
//last String authorName;
NewsPostPage({
required this.newsPostId,
/*required this.authorName*/
});
@override
_NewsPostPageState createState() => _NewsPostPageState();
}
class _NewsPostPageState extends State<NewsPostPage> {
newsPostDetails NewsPostDetails = newsPostDetails();
bool _isLoading = true;
String _error="";
TextEditingController _fullNameEditingController =
new TextEditingController();
TextEditingController _emailEditingController = new TextEditingController();
late DocumentReference newsPostRef;
late DocumentSnapshot newsPostSnap;
@override
void initState() {
tremendous.initState();
_getNewsPostDetails;
}
_getNewsPostDetails() async {
await DatabaseService().getNewsPostDetails(widget.newsPostId).then((res) {
setState(() {
NewsPostDetails = res;
_isLoading = false;
});
});
newsPostRef = FirebaseFirestore.occasion
.assortment('blogPosts')
.doc(widget.newsPostId);
newsPostSnap = await newsPostRef.get();
print(newsPostSnap.information);
}
@override
Widget construct(BuildContext context) {
return _isLoading
? Loading()
: Scaffold(
appBar: AppBar(
elevation: 0.0,
title: Textual content(NewsPostDetails.newsPostTitle! as String),
),
physique: Heart(
baby: ListView(
padding:
EdgeInsets.symmetric(horizontal: 20.0, vertical: 50.0),
kids: <Widget>[
Text(NewsPostDetails.newsPostTitle as String,
style: TextStyle(
fontSize: 40.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
SizedBox(height: 5.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Published on - ${NewsPostDetails.date as String}',
style:
TextStyle(fontSize: 14.0, color: Colors.grey)),
],
),
SizedBox(top: 40.0),
Textual content(NewsPostDetails.newsPostContent as String,
type: TextStyle(fontSize: 16.0)),
SizedBox(top: 110.0),
]),
));
}
}
Assist is extremely appreciated!
[ad_2]
