강산들
강산들

Reputation: 23

Class 'Future<DocumentSnapshot<Map<String, dynamic>>>' has no instance method '[]'

I'm making the profile screen(for user to change his profile image). Whenever the profile image is changed, the user's profile url in the Firestore DB changes through Firesbase Storage. And i have to reflect this change, so I brought the user doc(in Firestore DB) to stream. And I want to use the value of the field 'profileurl' in the user doc, but it doesn't work with the error below. I entered coding for the first time with a flutter. Please excuse if the question is stupid.

The error is

" Class 'Future<DocumentSnapshot<Map<String, dynamic>>>' has no instance method '[]'. Receiver: Instance of 'Future<DocumentSnapshot<Map<String, dynamic>>>' Tried calling: "

Here is the firestore. widget pickImage (using Imagepicker package) is well working, so i didnt write a code in this qu

enter image description here

Here is my Code.

class Ppage5_on extends StatefulWidget {
  const Ppage5_on({Key? key}) : super(key: key);

  @override
  State<Ppage5_on> createState() => _Ppage5_onState();
}

class _Ppage5_onState extends State<Ppage5_on> {

  @override
  initState() {
    userData = FirebaseFirestore.instance 
        .collection('member')
        .doc(FirebaseAuth.instance.currentUser!.email!).get();
    super.initState();
  }

  var userData;
  var ProfileUrl = "";
  final userQuery = FirebaseFirestore.instance
      .collection('member')
      .where('email', isEqualTo: FirebaseAuth.instance.currentUser!.email!);

  Widget _buildBody(BuildContext context) {
    return StreamBuilder<QuerySnapshot> (
      stream: userQuery.snapshots(),
      builder: (context, snapshot) {
        ///I think the error is occuered on here 
        setState(() {
          ProfileUrl = userData['profileUrl'];
        });
        if (!snapshot.hasData) return LinearProgressIndicator();
        return Scaffold(
            child: Column(
              children: [
                    ProfileUrl == ""
                        ? Stack( ...)
                        : Stack(children: [
                            CircleAvatar(
                                radius: 50,
                                backgroundImage: NetworkImage(
                                    ProfileUrl) ...} //Widget _buildbody end

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: _buildBody(context)
    );
  }
}

Upvotes: 0

Views: 642

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12353

Change this line to look like this:

ProfileUrl = snapshot.data()?['profileUrl'];

Upvotes: 1

Related Questions