hi every one hope you doing well I have a code to develop an app by flutter language but this error appears to me in list.dart. this list.dart file should display the posts from firebase with the images if there are.
and I don't know the answer
this is the code
import 'package:flutter/material.dart';
import 'package:insight_software/models/postModel.dart';
import 'package:insight_software/other_screen/userFunction.dart';
import 'package:provider/provider.dart';
import '../models/user.dart';
class ListPost extends StatefulWidget {
ListPost({Key? key}) : super(key: key);
@override
_ListPostState createState() => _ListPostState();
}
class _ListPostState extends State<ListPost> {
UserService _userService = UserService();
@override
Widget build(BuildContext context) {
List<postModel?> posts = Provider.of<List<postModel?>>(context);
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final Post = posts[index];
return StreamBuilder(
stream: _userService.getUserInfo(Post!.creator),
builder:
(BuildContext context, AsyncSnapshot<UserModel?> snapshot) {
// if (!snapshot.hasData) {
// return Center(child: CircularProgressIndicator());
// }
return ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
child: Row(
children: [
snapshot.data!.profileImageUrl != ''
? CircleAvatar(
radius: 20,
backgroundImage:
NetworkImage(snapshot.data!.profileImageUrl))
: Icon(
Icons.person,
size: 30,
),
SizedBox(width: 10),
Text(snapshot.data!.name),
],
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(Post.post),
SizedBox(height: 20),
Text(Post.timestamp.toDate().toString())
],
),
),
Divider(),
],
),
);
});
},
);
}
}
who is knowing how I can solve this problem please help me.
my postModel class:
import 'package:cloud_firestore/cloud_firestore.dart';
class postModel {
final String id;
final String creator;
final String post;
final Timestamp timestamp;
postModel(
{required this.id,
required this.creator,
required this.post,
required this.timestamp});
}
my userModel class:
class UserModel {
final String id;
final String bannerImageUrl;
final String profileImageUrl;
final String name;
final String email;
UserModel({
required this.id,
required this.bannerImageUrl,
required this.profileImageUrl,
required this.name,
required this.email,
});
}
and this is the getUserInfo
UserModel? _userFromFirebaseSnapshot(DocumentSnapshot snapshot) {
return snapshot != null
? UserModel(
id: snapshot.id,
name: snapshot.get('First Name') ?? '',
profileImageUrl: snapshot.get('profileImageUrl') ?? '',
bannerImageUrl: snapshot.get('bannerImageUrl') ?? '',
email: snapshot.get('email') ?? '',
)
: null;
}
Stream<UserModel?> getUserInfo(uid) {
return FirebaseFirestore.instance
.collection("Users")
.doc(uid)
.snapshots()
.map(_userFromFirebaseSnapshot);
}
CodePudding user response:
This error occurs when you use a bang operator (!) on a nullable instance which wasn't initialized.
String? foo; // Nullable String
void main() {
var len = foo!.length; // Runtime error: Null check operator used on a null value
}
For more details, you can see this answer.
So you can update your code:
stream: _userService.getUserInfo(Post!.creator),
with stream: _userService.getUserInfo(Post?.creator ?? ''),
and snapshot.data!.profileImageUrl != ''
with (snapshot.data?.profileImageUrl ?? '') != ''
and NetworkImage(snapshot.data!.profileImageUrl))
with NetworkImage(snapshot.data?.profileImageUrl) ?? '')
and Text(snapshot.data!.name),
with Text(snapshot.data?.name ?? ''),
CodePudding user response:
Error: Null check operator used on a null value
Two way you solve this.
in your model class set variable nullable
final String? profileImageUrl;
Use ?.
Or
Set default value if null.
profileImageUrl ?? ''
if value is null => ?? ''.