Exception caught by widgets library – Flutter

Issue

As I mentioned at the title, I got this error:

Exception caught by widgets library 
Closure call with mismatched arguments: function '[]'
Receiver: Closure: () => Map<String, dynamic> from Function 'data':.
Tried calling: []("imageURL")
Found: []() => Map<String, dynamic>

I have been trying to use it to get data from firestore and show it on my app page. But I can’t get the data from collection, especially for images. I referenced this tutorial from youtube. Even though I’ve done everything same but I couldn’t handle it. Maybe bc of version. I’d be glad if you help me.

class _HomeState extends State<Home> {
  PostService postService = new PostService();
  Stream postStream;


  //Stream postsStream;
  Widget postsList() {
    return SingleChildScrollView(
      child: postStream != null
        ? Column(
          children: <Widget>[
         StreamBuilder(
           //stream: postStream,
           stream: postStream,
           builder: (context, snapshot)
           {
             if(snapshot.data == null) return CircularProgressIndicator();
        
             return ListView.builder(
                 padding: EdgeInsets.symmetric(horizontal:16.0),
                 itemCount: snapshot.data.docs.length,
                 shrinkWrap: true,
                 itemBuilder: (context, index) {
                   return PostTile(
                     imgUrl: snapshot.data.docs[index].data['imageURL'],
                     title: snapshot.data.docs[index].data['postTitle'],
                     desc: snapshot.data.docs[index].data['postDesc'],
                     city: snapshot.data.docs[index].data['cityName'],

                   );
                 });
           }),
        ],
        ): Container(
    alignment: Alignment.center,
    child: CircularProgressIndicator(),
    ),
    );
  }

  @override
  void initState() {

    postService.getPostData().then((result) {
        setState(() {
        postStream = result;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Ana Sayfa'),
        backgroundColor: Colors.amber,
        elevation: 0.0,
        actions: <Widget>[
          FlatButton.icon(
            icon: Icon(Icons.group_rounded),
            label: Text(''),
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => KullaniciSayfasi()));
            },
          ),
        ],
      ),
      body: postsList(),
      floatingActionButton: Container(
        padding: EdgeInsets.symmetric(vertical: 10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
     

 children: <Widget>[
        FloatingActionButton(
          onPressed: () {
            //Ekleme butonuna basıldığında
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => CreatePost()));
          },
          child: Icon(Icons.add),
        )
      ],
    ),
  ),
);

}
}

Code for post service

import 'package:cloud_firestore/cloud_firestore.dart';


class PostService{

  Future<void> addData(postData) async{
  FirebaseFirestore.instance.collection("posts").add(postData).catchError((e){
     print(e);
    });
  }

  getPostData() async{

    return await FirebaseFirestore.instance.collection("posts").snapshots();
  }
}

Solution

There was a breaking change on firebase plugins and many things have changed. E.g i see you’re doing snapshot.data.docs[index].data['imageURL'] this has been changed to snapshot.data.docs[index].data()['imageURL']. Kindly check the docs for the updated API refrences

Answered By – Chiziaruhoma Ogbonda

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *