0

When I run the code below, all I am getting now is. I copied this code from another this post. This seems to work, prior to the last code I had, but I still am getting no success on retrieving any data. I tried the other things in the same post but I had no luck.

The method 'data' was called on null.
Receiver: null
Tried calling: data()
void main()  {

  runApp(myApp());
}

class myApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FirstRoute(title: 'First Route'),
    );
  }
}

class FirstRoute extends StatefulWidget {
  FirstRoute({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _FirstRouteState createState() => _FirstRouteState();
}

class _FirstRouteState extends State<FirstRoute> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("test"),
        ),
        body: FutureBuilder(
          future: getData(),
          builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return Column(
                children: [
                  Container(
                    height: 27,
                    child: Text(
                      "Name: ${snapshot.data.data()['Name']}",
                      overflow: TextOverflow.fade,
                      style: TextStyle(fontSize: 20),
                    ),
                  ),
                ],
              );
            } else if (snapshot.connectionState == ConnectionState.none) {
              return Text("No data");
            }
            return CircularProgressIndicator();
          },
        ));
  }

  Future<DocumentSnapshot> getData() async {
    await Firebase.initializeApp();
    return await FirebaseFirestore.instance
        .collection("test")
        .doc()
        .get();
  }
}

On the firestore website it says that there are some reads and there's also snapshots listeners active, but the error still persists. Any recommendations and tips would be great. Thanks!

Emanuel
  • 5
  • 4
  • 1
    To answer the title of the question: No. – daddygames Mar 16 '21 at 17:37
  • Thanks for your answer. But do you know what could be the problem then? I've seen hundreds of tutorials, articles, etc to make I've properly initialized the Firestore but I still get an error. Prior to this I could see that I was getting snapshots, but now I can't seem to be getting anything. – Emanuel Mar 16 '21 at 17:43
  • @Emanuel: I recommend first updating the title of your question to talk about the actual problem you're having. Without that, daddygames' comment is the answer, and I doubt that's what you're looking for. To help beyond that, edit your question to show the [minimal code that reproduces the problem](http://stackoverflow.com/help/mcve) (read the link please, as following the guidance in there maximizes the chances that someone can help). – Frank van Puffelen Mar 16 '21 at 17:47
  • Thank you @FrankvanPuffelen I've updated the question title and the context + added some code. – Emanuel Mar 16 '21 at 18:18
  • Did you restart (instead of hot reloading) the app? Also note that there are quite a few existing posts about this error message already, so you might want to check those out and update your question to show what you've already tried from those answer. – Frank van Puffelen Mar 16 '21 at 20:25
  • @FrankvanPuffelen Thank you for all your help so far! I've used different code, from a different post and I've got a new error now. I've updated the post itself and the code. – Emanuel Mar 17 '21 at 00:37

0 Answers0