[FlowListView] NullReferenceException when refresh the list
Steps to reproduce:
- Load the collection from database or another source
- Remove a item from database
- Refresh the list using Pull-to-Refresh for example
Result: App craches.
Observations:
- If the new refresh returns the same quantity of items, so the app doesn't crache;
- If the new refresh retunns zero items, so the app doesn't crache;
- If I leave the page and navigate to it again, the error does not occur
Versions: DLToolkit.Forms.Controls.FlowListView: 2.0.11 Xamarin.Forms: 3.4.0.1008975
Error:
11-20 23:38:07.111 I/MonoDroid(10001): System.NullReferenceException: Object reference not set to an instance of an object.
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Internals.DataTemplateExtensions.CreateContent (Xamarin.Forms.DataTemplate self, System.Object item, Xamarin.Forms.BindableObject container) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\DataTemplateExtensions.cs:19
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].ActivateContent (System.Int32 index, System.Object item) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:534 11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].CreateContent (System.Int32 index, System.Object item, System.Boolean insert) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:543
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].GetOrCreateContent (System.Int32 index, System.Object item) [0x00023] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:602 11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].get_Item (System.Int32 index) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:337
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellsFromPosition (System.Int32 position, System.Int32 take) [0x0003b] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:538
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellForPosition (System.Int32 position) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:453
11-20 23:38:07.111 I/MonoDroid(10001): at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x0006d] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:225
11-20 23:38:07.111 I/MonoDroid(10001): at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x00018] in <263adecfa58f4c449f1ff56156d886fd>:0
11-20 23:38:07.111 I/MonoDroid(10001): at (wrapper dynamic-method) System.Object.287e09fa-fd7a-4426-ae0c-4254aa73f3b9(intptr,intptr,int,intptr,intptr)
Thanks in advance
same here, any one resolved this??
Finally I found the cause, add mode as OneWayToSource in the FlowListView control that keeps the th total count of the list which is a reasonable way like this:
FlowTotalRecords="{Binding yourViewmodel.Count, Mode=OneWayToSource}"
TL; DR;
Set FlowLoadingTemplate because that is what's probably causing NullReferenceException
A bit more explanation:
I've analyzed this and come to the conclusion that it has something to do with the FlowTotalRecords but your solution just prevents it from having a different value from the default one because of how OneWayToSource works and also because FlowTotalRecords is never changed inside the control, it just uses the value of the binding, so OneWay should always be the right mode of the binding.
The problem is that when you set FlowTotalRecords then this line is reached thus adding FlowFloadingModel to the list. Then if you see the FlowDataTemplateSelector here you'll see that it returns the FlowLoadingTemplate of the current FlowListView when the item is an IFlowLoadingModel; so you must set the FlowLoadingTemplate in order to render that correctly.
In conclusion if you enable FlowIsLoadingInfiniteEnabled and set FlowTotalRecords take into consideration setting the FlowLoadingTemplate otherwise null reference of FlowLoadingTemplate will take place.