Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'DocumentSnapshot' is not a subtype of type 'Map<String, dynamic>'

This is my profile Page
I also tried snapshot.data() and also tried converting it to Map data by seeing some other StackOverflow answers. Please help me to resolve this issue.

The error is in line number 63. I am not sure how to get that correct. Actually, in the tutorial, they showed for from document but I have to use FromMap But I am not sure How to use that

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:justpoll/screens/profile/edit_profile.dart';
import 'package:justpoll/screens/profile/profile_nav_bar/settings.dart';
import 'package:justpoll/screens/profile/profile_nav_bar/help.dart';
import 'package:justpoll/Constants.dart';
import 'package:justpoll/widgets/progress.dart';
import 'package:justpoll/libmodels/user_model.dart';
import 'package:justpoll/libservices/authentication_service.dart';
import 'package:justpoll/screens/home_page/nav.dart';

class Profile2 extends StatefulWidget {
  final String profileId;

  Profile2({this.profileId});
  @override
  _Profile2State createState() => new _Profile2State();
}

final userRef = FirebaseFirestore.instance.collection("users");

class _Profile2State extends State<Profile2> {
  buildProfileButton() {
    return Text("profile Button");
  }

  Column buildCountColumn(String lable, int count) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          count.toString(),
          style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold),
        ),
        Container(
          margin: EdgeInsets.only(top: 4.0),
          child: Text(
            lable,
            style: TextStyle(
              color: Colors.grey,
              fontSize: 15.0,
              fontWeight: FontWeight.w400,
            ),
          ),
        )
      ],
    );
  }

  buiildProfileHeader() {
    return FutureBuilder(
      future: userRef.doc(widget.profileId).get(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return circularProgress();
        }

        UserModel user = UserModel.fromMap(snapshot.data.data);

        return Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: [
              Row(
                children: [
                  CircleAvatar(
                    radius: 40.0,
                    backgroundColor: Colors.grey,
                    backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                  ),
                  Expanded(
                    flex: 1,
                    child: Column(
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            buildCountColumn("posts", 0),
                            buildCountColumn("follower", 0),
                            buildCountColumn("following", 0),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            buildProfileButton(),
                          ],
                        )
                      ],
                    ),
                  ),
                ],
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 12.0),
                child: Text(
                  user.username,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 4.0),
                child: Text(
                  user.name,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 2.0),
                child: Text(
                  user.bio,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        iconTheme: IconThemeData(color: MyColors.black),
        backgroundColor: Colors.white,
        actions: [
          Padding(
            padding: const EdgeInsets.all(13.0),
            child: GestureDetector(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => EditProfile(),
                  ),
                );
              },
              child: Icon(
                Icons.edit,
                color: MyColors.black,
                size: 26,
              ),
            ),
          ),
        ],
      ),
      drawer: new Drawer(
          child: new ListView(
        children: <Widget>[
          new ListTile(
            leading: Icon(Icons.settings),
            title: new Text('Settings'),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => SettingsScreen()),
              );
            },
          ),
          new ListTile(
            leading: Icon(Icons.bookmark),
            title: new Text('Bookmarks'),
            onTap: () {},
          ),
          new ListTile(
            leading: Icon(Icons.drafts),
            title: new Text('Drafts'),
            onTap: () {},
          ),
          new ListTile(
            leading: Icon(Icons.archive),
            title: new Text('Archive'),
            onTap: () {},
          ),
          new ListTile(
            leading: Icon(Icons.help),
            title: new Text('help'),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => HelpPage()),
              );
            },
          ),
        ],
      )),
      body: ListView(
        children: <Widget>[
          buiildProfileHeader(),
        ],
      ),
    );
  }
}

And This is my user module

class UserModel {
  String email;
  String uid;
  String username;
  String name;
  DateTime timestamp;
  bool isPrivate;
  String photoUrl;
  String gender;
  String bio;
  String birthday;
  Map followers;
  Map following;

  UserModel({
    this.email,
    this.uid,
    this.username,
    this.timestamp,
    this.name,
    this.photoUrl,
    this.isPrivate,
    this.gender,
    this.bio,
    this.birthday,
    this.followers,
    this.following,
  });

  Map toMap(UserModel user) {
    var data = Map<String, dynamic>();

    data["uid"] = user.uid;
    data["username"] = user.username;
    data["email"] = user.email;
    data['name'] = user.name;
    data['photoUrl'] = user.photoUrl;
    data["timestamp"] = user.timestamp;
    data["isPrivate"] = user.isPrivate;
    data["gender"] = user.gender;
    data["bio"] = user.bio;
    data["birthday"] = user.birthday;
    data["followers"] = user.followers;
    data["following"] = user.following;
    return data;
  }

  UserModel.fromMap(Map<String, dynamic> mapData) {
    this.uid = mapData["uid"];
    this.username = mapData["username"];
    this.email = mapData["email"];
    this.name = mapData["name"];
    this.isPrivate = mapData["isPrivate"];
    this.gender = mapData["gender"];
    this.photoUrl = mapData["photoUrl"];
    this.bio = mapData["bio"];
    this.birthday = mapData["birthday"];
    this.followers = mapData["followers"];
    this.following = mapData["following"];
  }
}
like image 423
Ramith K S Avatar asked Oct 14 '25 08:10

Ramith K S


2 Answers

try this

var DocData = snapshot.data as DocumentSnapshot;

This gives you the data which can be accessed by

DocData['fieldname'];

you can use the fields like this

like image 151
Anirudh Agrawal Avatar answered Oct 17 '25 02:10

Anirudh Agrawal


try this;

UserModel user = UserModel.fromMap(snapshot.data.data());
buiildProfileHeader() {
  return FutureBuilder(
    future: userRef.doc(widget.profileId).get(),
    builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      if (!snapshot.hasData) {
        return circularProgress();
      }
      if (snapshot.hasError) {
        return Text("Something went wrong");
      }

      if (snapshot.connectionState == ConnectionState.done) {
        Map<String, dynamic> data = snapshot.data.data();
        UserModel user = UserModel.fromMap(data);
        return Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: [
              Row(
                children: [
                  CircleAvatar(
                    radius: 40.0,
                    backgroundColor: Colors.grey,
                    backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                  ),
                  Expanded(
                    flex: 1,
                    child: Column(
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            buildCountColumn("posts", 0),
                            buildCountColumn("follower", 0),
                            buildCountColumn("following", 0),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            buildProfileButton(),
                          ],
                        )
                      ],
                    ),
                  ),
                ],
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 12.0),
                child: Text(
                  user.username,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 4.0),
                child: Text(
                  user.name,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 2.0),
                child: Text(
                  user.bio,
                ),
              ),
            ],
          ),
        );
      }
    },
  );
}
like image 42
prahack Avatar answered Oct 17 '25 00:10

prahack