Trying to show data from Firebase Firestore

Viewed 80

I am trying to show data from my Firebase Firestore for which I created a Crud File.

I have tried different things in my body.dart file and I am continuously getting the circular progress bar and some error.

Accessing hidden method Lsun/misc/Unsafe;

multiple times

This is the body.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:masterpass/Screens/Passwords/components/password_list.dart';
import 'package:masterpass/Services/crud.dart';

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  CrudMethods crudMethods = new CrudMethods();

  QuerySnapshot passSnapshot;

  @override
  void initState() {
    super.initState();
    crudMethods.getData().then((result) {
      passSnapshot = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return PasswordList(passSnapshot: passSnapshot);
  }
}

Password_List.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:masterpass/Screens/Passwords/components/password_tile.dart';

class PasswordList extends StatelessWidget {
  const PasswordList({
    Key key,
    @required this.passSnapshot,
  }) : super(key: key);

  final QuerySnapshot<Object> passSnapshot;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: passSnapshot != null
          ? Column(
              children: [
                ListView.builder(
                  itemCount: passSnapshot.docs.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return PasswordTile(
                      username: passSnapshot.docs[index].get('username'),
                      website: passSnapshot.docs[index].get('url'),
                    );
                  },
                )
              ],
            )
          : Container(
              child: CircularProgressIndicator(),
            ),
    );
  }
}

This is the crud.dart

import 'package:cloud_firestore/cloud_firestore.dart';

class CrudMethods {
  Future<void> addData(passwordData) async {
    FirebaseFirestore.instance
        .collection("passwords")
        .add(passwordData)
        .catchError((e) {
      print(e);
    });
  }

  getData() async {
    return await FirebaseFirestore.instance.collection("passwords").get();
  }
}

For Better Understanding the Code: GitHub Repo

1 Answers

I found 2 Github issues related to the error you are getting, one in Firebase and one in Flutter, I believe the cause to be the latter one, so you might need to follow that thread for updates, since that is actually suppose to be an Android issue.

You can try the proposed workaround in the Firebase Github issue however, which is to add (or change if you have it already) this tag in your AndroidManifest.xml file:

<application android:allowBackup="false">

And then run the following:

flutter clean
flutter run
flutter create .
Related