0
 body: StreamBuilder<List<User>>(
        stream: readUsers(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Text('Error ${snapshot.error}');
          }
          if (snapshot.hasData) {
            final users = snapshot.data!;

            return ListView(
              children: users.map(buildUser).toList(),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      )),

The part where I am trying to pull data from firebase.

  Widget buildUser(User user) => ListTile(leading: Text('${user.bmi}'));

The buildUser widget I created.

  Stream<List<User>> readUsers() => FirebaseFirestore.instance
  .collection('users')
  .snapshots()
  .map((snapshot) =>
      snapshot.docs.map((doc) => User.fromJson(doc.data())).toList());

The readUser structure I created

I am able to print data to the database and one of these data is a double value named 'bmi'. When I continue the process, it stays in the snapshot.haserror loop and

type null is not a subtype of type 'String'

prints the error.

    class User {
  final double bmi;

  User(
      {
      required this.bmi});

  Map<String, dynamic> toJson() => {
        'bmi': bmi,
      };

  static User fromJson(Map<String, dynamic> json) => User(
      bmi: json['bmi']);
}
3
  • Could you specify where exactly error happen? Commented Dec 6, 2022 at 16:11
  • I am getting some values ​​on the previous page from the user. Then I go to the page with the error with a button. It also prints the error I asked in the question inside the page. Commented Dec 6, 2022 at 16:15
  • maybe your error come from another model , in this class your provided there is no string type at all Commented Dec 6, 2022 at 16:26

1 Answer 1

2

i am not 100% sure, but i had the same error, where the json did not have that specific field. Before you try to get the 'bmi' field of json, check if it exists:

json.containsKey("bmi") ? json['bmi'] : "missing field" 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately nothing has changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.