Zain Thaver
Zain Thaver

Reputation: 61

Flutter App Crashes on Simultaneous Transactions (cloud_firestore)

I am currently trying to make a basic counter app using flutter and cloud_firestore. However, whenever I increment the counter rapidly, the app is crashing and giving the following error.Caused by: java.lang.AssertionError: INTERNAL ASSERTION FAILED: A transaction object cannot be used after its update callback has been invoked. I have looked at several other people having a similar issue such as https://github.com/FirebaseExtended/flutterfire/issues/1216.I have attached my code,where there are all the items read from my database in a list.

Widget _buildListItem(BuildContext context,DocumentSnapshot doc)
{
  return ListTile(
    title: Row(children: <Widget>[
      Expanded(child: Text(doc['prayerName']),),

      Container(
        child: Text(doc['totalCount'].toString()),
      )
    ],) ,
    onTap: ()  {
   databaseReference.runTransaction((transaction) async {
        DocumentSnapshot counterChange = await transaction.get(doc.reference);
   await transaction.update(counterChange.reference,
            {
              'totalCount':counterChange['totalCount']+1,
            });

      } );

    }
  );
}
  Widget build(BuildContext context)
  {

return Scaffold(
  appBar: AppBar(title: Text('View Your Prayers')),
body:StreamBuilder(
  stream: Firestore.instance.collection('prayerRooms').snapshots(),
  builder: (context,snapshot)
    {
      if(!snapshot.hasData)return const Text("Loading..");
      return ListView.builder(
        itemBuilder: (context,index)=>
        _buildListItem(context,snapshot.data.documents[index]),
        itemCount: snapshot.data.documents.length,
                              );
    }
)
);
  }

Upvotes: 0

Views: 592

Answers (1)

Samuel Romero
Samuel Romero

Reputation: 1253

In the link your shared in your description, it is said that this is a known issue in the flutter binding library with the iOS devices. A possible solution is mention at the same post here or trying with this suggestion.

Upvotes: 1

Related Questions