Search code examples
flutterrealm

Flutter Realm Navigator causes Access to invalidated Results objects


I'm pretty new to Flutter and I'm writing a Flutter application with Realm. I have an ExpansionTile that when opened have a ListView of ListTiles. The ListTiles then call Navigator to go to the next page (WorkoutPage).

Problem I am having is whenever I perform Hot Reload on WorkoutPage, I get Error code: 2009 . Message: Access to invalidated Results objects.

Widget build(BuildContext context) {
    final realmServices = Provider.of<RealmServices>(context);
    // bunch of unrelated code
    return StreamBuilder<RealmResultsChanges<Workout>>(
    stream: workoutList(realmServices, currentUser!.id, routine.id.toString()),
    builder: (context, snapshot){
        final data = snapshot.data;
        final result = data.results;
        return ExpansionTile(
            // bunch of unrelated code
            children:[
            ListView.builder
            (
                shrinkWrap: true,
                itemCount: result.realm.isClosed ? 0 : result.length, 
                itemBuilder: (BuildContext context, int index) 
                { 
                    return (result[index].isValid && !result.realm.isClosed ? 
                    Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column (
                            mainAxisSize: MainAxisSize.min,
                            children: [
                                ListTile(
                                    title: Text(result[index].workoutName),
                                    onTap: () {
                                        Navigator.pushReplacement(context,MaterialPageRoute(builder(context) => WorkoutPage(isRoutine:false, workoutId: result[index].id.toString(),)));
                                    },
                                ),
                            ],
                        ),
                    ) 
                    : 
                    Container());
                },
            )
        ],
    );
}

I currently close realm in my dispose function in RealmServices and when I comment that out and keep the connection open, the error disappears. But I don't think that is the correct way to go about it since I am now keeping the connection open the entire time.

I do plan on using RealmServices in my other screens, so I am not sure what is the best way to go about it.

Any help would be greatly appreciated.


Solution

  • Solved this issue by just extracting the data into variables before navigating.